管理 Conda 建立的 Python 虚拟环境
换了新电脑 (Windows 10), 打算用 Anaconda
管理自己的包.
下载安装
下载 Anaconda
下载最新版的 Anaconda.
安装 Anaconda
直接把 Anaconda 安装在目标位置.
这个时候发现安装目录下, Anaconda 大量内容丢失, 我的判断标准是: 没有 \Script
文件夹.
为了安全, 下载安装 VS2019, 勾选 python 组件并安装. 找到 VS2019 下载好的文件夹, 移动到 Anaconda 安装位置即可.
新建虚拟环境
打开命令行, 输入:
...>conda activate
则进入基础虚拟环境 (base)
, 环境名会出现在在命令行路径的开头
新建虚拟环境 new_env1
, 则输入:
(base) ...>conda create -n new_env1
如果没有 -n
命令, 则:
(base) ...>conda create new_env1
CondaValueError: The target prefix is the base prefix. Aborting.
正常情况下, 应该显示:
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: %Anaconda安装目录%\envs\tmp
Proceed ([y]/n)?
其中 %Anaconda安装目录%
指 Anaconda 的安装目录, 即 \Script
文件夹所在的目录.
输入 y
, Enter
创建环境.
更改新虚拟环境的位置.
打开 C:\Users\用户名\.condarc
, 其中 用户名
为当前用户的用户名.
向 .condarc
中添加如下内容:
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- defaults
show_channel_urls: true
envs_dirs:
- %自定义路径%\conda\envs
- %Anaconda安装目录%\envs
- C:\Users\用户名\.conda\envs
- C:\Users\用户名\AppData\Local\conda\conda\envs
pkgs_dirs:
- %自定义路径%\conda\pkgs
- %Anaconda安装目录%\pkgs
- C:\Users\用户名\.conda\pkgs
- C:\Users\用户名\AppData\Local\conda\conda\pkgs
保存.
默认的 .condarc
文件中仅有 {}
两个字符, 直接删除并粘贴上述内容即可. 不放心的话, 可以先输入
(base) ...>conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
(base) ...>conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
(base) ...>conda config --set show_channel_urls yes
来添加 channels
和 show_channel_urls
两个变量.
此时可以 在 base
环境下 查看 Anaconda 环境信息:
(base) ...>conda activate new_env1
(new_env1) ...>conda info
接下来, 创建另一个虚拟环境 new_env2
(new_env1) ...>conda create -n new_env2
可以发现, environment location
已经发生变化, 此时可以放心输入 y
, Enter
创建环境.
查阅资料, 还可以使用 --prefix=
命令指定路径.
虚拟环境的切换
虚拟环境内外没有什么差别, 都可以使用 conda [-h] [-V] command ...
等命令, ping
等命令也可以使用, 只是 python 解释器有所不同.
值得一提的是, 每次使用
(last_env) ...>conda activate next_env
来进入下一个虚拟环境 next_env
后再使用
conda deactivate
命令退出时, 会退出到上一个虚拟环境 last_env
, 类似出栈入栈.
向虚拟环境中添加包
先把试错的过程放上来
下载 pygame 安装包
先来到和 Anaconda 相同目录的环境 new_env1
, 并下载 pygame
(base) ...>conda new_env1
(new_env1) ...>conda install pygame
Collecting package metadata (current_repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
Collecting package metadata (repodata.json): done
Solving environment: failed with initial frozen solve. Retrying with flexible solve.
PackagesNotFoundError: The following packages are not available from current channels:
- pygame
Current channels:
- (URL)
-
To search for alternate channels that may provide the conda package you're
looking for, navigate to
https://anaconda.org
and use the search bar at the top of the page.
(URL)
是conda的源服务器
无法下载, 只能使用 pip
命令.
(new_env1) ...>pip install pygame
Collecting pygame
Downloading pygame-1.9.6-cp36-cp36m-win_amd64.whl (4.3 MB)
只不过网络不太好, 下载了好几次后系统才显示安装成功.
为了验证是否仅在当前虚拟环境下安装, 进入 base
环境查看:
(new_env1) ...>conda activate base
(base) pip list
Package Version
---------------------------------- ---------
...
pygame 1.9.6
...
很明显, 此时 pip
仍然是全局命令.
于是删除这个包
(base) ...>pip uninstall pygame
输入 y
, Enter
后成功删除.
然后在又在 new_env2
, base
环境中反复尝试:
(base) ...>conda activate new_env2
(new_env2) ...>pip install pygame
Collecting pygame
Using cached pygame-1.9.6-cp37-cp37m-win_amd64.whl (4.3 MB)
Installing collected packages: pygame
Successfully installed pygame-1.9.6
不用下载了, 但 pip
命令都是全局的. 在网上搜索众多解决方案无果.
conda install
命令安装包
pip
命令无效后, 先尝试用 conda install
命令安装其他包, 比如说 matplotlib
, 安装在创建于自定义路径下的环境 new_env2
中:
(new_env2) ...>conda install matplotlib
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: %自定义路径%\conda\envs\new_env2
added / updated specs:
- matplotlib
The following packages will be downloaded:
package | build
---------------------------|-----------------
certifi-2019.11.28 | py38_0 149 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
cycler-0.10.0 | py_2 9 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
freetype-2.10.0 | h563cfd7_1 476 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
icu-64.2 | he025d50_1 14.1 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
jpeg-9c | hfa6e2cd_1001 314 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
kiwisolver-1.1.0 | py38he980bc4_0 61 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libclang-9.0.1 |default_hf44288c_0 20.8 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libpng-1.6.37 | h7602738_0 1.3 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
matplotlib-3.1.3 | py38_0 7 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
matplotlib-base-3.1.3 | py38h2981e6d_0 6.5 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
numpy-1.17.5 | py38hc71023c_0 4.7 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
pip-20.0.2 | py38_1 1.9 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
pyparsing-2.4.6 | py_0 59 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
pyqt-5.12.3 | py38h6538335_1 4.7 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
python-3.8.1 | he1f5543_2 18.9 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
python-dateutil-2.8.1 | py_0 220 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
qt-5.12.5 | h7ef1ec2_0 104.4 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
setuptools-45.1.0 | py38_0 664 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
six-1.14.0 | py38_0 23 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
sqlite-3.30.1 | hfa6e2cd_0 1.1 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
tornado-6.0.3 | py38hfa6e2cd_0 641 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
vs2015_runtime-14.16.27012 | hf0eaf9b_1 1.1 MB defaults
wheel-0.34.2 | py38_0 61 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
wincertstore-0.2 | py38_1003 13 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
zlib-1.2.11 | h2fa13f4_1006 236 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
------------------------------------------------------------
Total: 182.5 MB
The following NEW packages will be INSTALLED:
ca-certificates anaconda/cloud/conda-forge/win-64::ca-certificates-2019.11.28-hecc5488_0
certifi anaconda/cloud/conda-forge/win-64::certifi-2019.11.28-py38_0
cycler anaconda/cloud/conda-forge/noarch::cycler-0.10.0-py_2
freetype anaconda/cloud/conda-forge/win-64::freetype-2.10.0-h563cfd7_1
icu anaconda/cloud/conda-forge/win-64::icu-64.2-he025d50_1
intel-openmp pkgs/main/win-64::intel-openmp-2019.4-245
jpeg anaconda/cloud/conda-forge/win-64::jpeg-9c-hfa6e2cd_1001
kiwisolver anaconda/cloud/conda-forge/win-64::kiwisolver-1.1.0-py38he980bc4_0
libblas anaconda/cloud/conda-forge/win-64::libblas-3.8.0-14_mkl
libcblas anaconda/cloud/conda-forge/win-64::libcblas-3.8.0-14_mkl
libclang anaconda/cloud/conda-forge/win-64::libclang-9.0.1-default_hf44288c_0
liblapack anaconda/cloud/conda-forge/win-64::liblapack-3.8.0-14_mkl
libpng anaconda/cloud/conda-forge/win-64::libpng-1.6.37-h7602738_0
matplotlib anaconda/cloud/conda-forge/win-64::matplotlib-3.1.3-py38_0
matplotlib-base anaconda/cloud/conda-forge/win-64::matplotlib-base-3.1.3-py38h2981e6d_0
mkl pkgs/main/win-64::mkl-2019.4-245
numpy anaconda/cloud/conda-forge/win-64::numpy-1.17.5-py38hc71023c_0
openssl anaconda/cloud/conda-forge/win-64::openssl-1.1.1d-hfa6e2cd_0
pip anaconda/cloud/conda-forge/win-64::pip-20.0.2-py38_1
pyparsing anaconda/cloud/conda-forge/noarch::pyparsing-2.4.6-py_0
pyqt anaconda/cloud/conda-forge/win-64::pyqt-5.12.3-py38h6538335_1
python anaconda/cloud/conda-forge/win-64::python-3.8.1-he1f5543_2
python-dateutil anaconda/cloud/conda-forge/noarch::python-dateutil-2.8.1-py_0
qt anaconda/cloud/conda-forge/win-64::qt-5.12.5-h7ef1ec2_0
setuptools anaconda/cloud/conda-forge/win-64::setuptools-45.1.0-py38_0
six anaconda/cloud/conda-forge/win-64::six-1.14.0-py38_0
sqlite anaconda/cloud/conda-forge/win-64::sqlite-3.30.1-hfa6e2cd_0
tornado anaconda/cloud/conda-forge/win-64::tornado-6.0.3-py38hfa6e2cd_0
vc pkgs/main/win-64::vc-14.1-h0510ff6_4
vs2015_runtime pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_1
wheel anaconda/cloud/conda-forge/win-64::wheel-0.34.2-py38_0
wincertstore anaconda/cloud/conda-forge/win-64::wincertstore-0.2-py38_1003
zlib anaconda/cloud/conda-forge/win-64::zlib-1.2.11-h2fa13f4_1006
Proceed ([y]/n)?
输入 Enter
以下载.
奇怪, 居然要下载 python
和 pip
, 难道之前一直在用 base
环境下的 python 解释器? 那就说的通了. 打开 new_env2
的安装目录, 的确多了不少东西, 而 new_env1
的安装目录中空空荡荡, 我的判断标准是: 没有 python.exe
程序文件.
那么再试试下载 pygame
:
(new_env2) ...>pip install pygame
Collecting pygame
Downloading pygame-1.9.6-cp36-cp36m-win_amd64.whl (4.3 MB)
发现又需要重新从网上下载, 网络状况不好, 下载中断, 安装失败.
那么回到 new_env1
再试试, 这次下载 python
:
(new_env2) ...>conda activate new_env1
(new_env1) ...>conda install conda install python
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: %自定义路径%\conda\envs\new_env1
added / updated specs:
- python
The following packages will be downloaded:
package | build
---------------------------|-----------------
certifi-2019.11.28 | py38_0 149 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
pip-20.0.2 | py38_1 1.9 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
python-3.8.1 | he1f5543_2 18.9 MB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
setuptools-45.1.0 | py38_0 664 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
vs2015_runtime-14.16.27012 | hf0eaf9b_1 1.1 MB defaults
wheel-0.34.2 | py38_0 61 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
wincertstore-0.2 | py38_1003 13 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
------------------------------------------------------------
Total: 22.8 MB
The following NEW packages will be INSTALLED:
ca-certificates anaconda/cloud/conda-forge/win-64::ca-certificates-2019.11.28-hecc5488_0
certifi anaconda/cloud/conda-forge/win-64::certifi-2019.11.28-py38_0
openssl anaconda/cloud/conda-forge/win-64::openssl-1.1.1d-hfa6e2cd_0
pip anaconda/cloud/conda-forge/win-64::pip-20.0.2-py38_1
python anaconda/cloud/conda-forge/win-64::python-3.8.1-he1f5543_2
setuptools anaconda/cloud/conda-forge/win-64::setuptools-45.1.0-py38_0
sqlite anaconda/cloud/conda-forge/win-64::sqlite-3.30.1-hfa6e2cd_0
vc pkgs/main/win-64::vc-14.1-h0510ff6_4
vs2015_runtime pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_1
wheel anaconda/cloud/conda-forge/win-64::wheel-0.34.2-py38_0
wincertstore anaconda/cloud/conda-forge/win-64::wincertstore-0.2-py38_1003
Proceed ([y]/n)?
Downloading and Extracting Packages
pip-20.0.2 | 1.9 MB | ######################################################################### | 100%
python-3.8.1 | 18.9 MB | ######################################################################### | 100%
wincertstore-0.2 | 13 KB | ######################################################################### | 100%
setuptools-45.1.0 | 664 KB | ######################################################################### | 100%
wheel-0.34.2 | 61 KB | ######################################################################### | 100%
certifi-2019.11.28 | 149 KB | ######################################################################### | 100%
vs2015_runtime-14.16 | 1.1 MB | ######################################################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
(new_env1) ...>pip install pygame
Collecting pygame
Using cached pygame-1.9.6-cp37-cp37m-win_amd64.whl (4.3 MB)
Installing collected packages: pygame
Successfully installed pygame-1.9.6
进入其他环境查看,
(new_env1) ...>conda activate base
(base) ...>pip list
就没有发现 pygame
了.
通过已下载的包安装
还不会.
复制虚拟环境
后来发现, 完全可以用克隆的方式新建一个虚拟环境, 这样也免得要从头下载了
(base) ...>conda create -n new_env3 --clone base
Source: %Anaconda安装目录%
Destination: %自定义路径%\conda\envs\mylibrary
The following packages cannot be cloned out of the root environment:
- defaults/win-64::conda-4.8.1-py37_0
- defaults/win-64::conda-build-3.18.9-py37_3
- defaults/win-64::conda-env-2.6.0-1
Packages: 275
Files: 508
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
方便了许多.
另一种复制虚拟环境的方式是使用 --file
参数, 其后输入一个文件名
输入:
(base)...> conda create -n new_env3
即可
删除虚拟环境
为了保证系统盘的空间充裕, 删除 new_env1
环境:
(base) ...>conda remove -n new_env1 --all
Remove all packages in environment %Anaconda安装目录%\envs\new_env1:
## Package Plan ##
environment location: %Anaconda安装目录%\envs\new_env1
The following packages will be REMOVED:
...
Proceed ([y]/n)? y
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
移动自定义路径
想再多添加一个自定义路径好像是不行的? 但是直接改路径名和环境包名就可以了.