python创建虚拟环境
python创建虚拟环境的目的
创建虚拟环境是为了让项目运行在一个独立的局部的Python环境中,这样可以让不同项目用不同的python模块,使得不同环境不会相互干扰。
因为虚拟环境可以使一个Python程序拥有独立的库library和解释器interpreter,而不用与其他Python程序共享统一个library和interpreter。
打个比方:如果程序A、程序B是用同一个python环境,程序A需要某个库的1.0版本,而程序B需要同样这个库的2.0版本,那么程序A能执行,则程序B就不能执行。
conda创建虚拟环境方法
1、查看已有虚拟环境
先进入命令行窗口win+R
,输入cmd
罗列当前已有的虚拟环境conda env list
C:\Users\Administrator>conda env list
# conda environments:
#
base * D:\r\Anaconda3
professior D:\r\Anaconda3\envs\professior
pycharm book D:\r\Anaconda3\envs\pycharm book
我这台电脑有3个环境,分别是 base
、professior
、pycharm book
其中 base
可以看到有个*,代表的是它为最根本的环境。
2、创建环境
查看conda create
命令
C:\Users\Administrator>conda create --help
usage: conda-script.py create [-h] [--clone ENV] [-n ENVIRONMENT | -p PATH]
[-c CHANNEL] [--use-local] [--override-channels]
[--repodata-fn REPODATA_FNS]
[--strict-channel-priority]
[--no-channel-priority]
[--no-deps | --only-deps] [--no-pin] [--copy]
[--no-shortcuts] [-C] [-k] [--offline] [-d]
[--json] [-q] [-v] [-y] [--download-only]
[--show-channel-urls] [--file FILE]
[--no-default-packages] [--dev]
[package_spec [package_spec ...]]
Create a new conda environment from a list of specified packages. To use the created environment, use 'source activate envname' look in that directory first. This command requires either the -n NAME or -p PREFIX option.
...
从上图可以看到
Create a new conda environment from a list of specified packages. To use the created environment, use 'source activate envname' look in that directory first. This command requires either the -n NAME or -p PREFIX option.
翻译:
从指定的包列表中创建一个新的conda环境。要使用创建的环境,使用'source activate envname'首先在该目录中查找。这个命令需要-n名称或-p前缀选项。
创建名为frog_test
的虚拟环境,指定python版本3.7
。
conda create -n frog_test python=3.7
C:\Users\Administrator>conda create -n frog_test python=3.7
Collecting package metadata (current_repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 4.7.12
latest version: 4.8.3
Please update conda by running
$ conda update -n base -c defaults conda
## Package Plan ##
environment location: D:\r\Anaconda3\envs\frog_test
added / updated specs:
- python=3.7
The following packages will be downloaded:
package | build
---------------------------|-----------------
ca-certificates-2020.6.24 | 0 125 KB defaults
certifi-2020.6.20 | py37_0 156 KB defaults
openssl-1.1.1g | he774522_0 4.8 MB defaults
pip-20.1.1 | py37_1 1.7 MB defaults
python-3.7.7 | h81c818b_4 14.3 MB defaults
setuptools-49.2.0 | py37_0 760 KB defaults
sqlite-3.32.3 | h2a8f88b_0 802 KB defaults
vs2015_runtime-14.16.27012 | hf0eaf9b_3 1.2 MB defaults
wheel-0.34.2 | py37_0 66 KB defaults
zlib-1.2.11 | h62dcd97_4 113 KB defaults
------------------------------------------------------------
Total: 24.0 MB
The following NEW packages will be INSTALLED:
ca-certificates pkgs/main/win-64::ca-certificates-2020.6.24-0
certifi pkgs/main/win-64::certifi-2020.6.20-py37_0
openssl pkgs/main/win-64::openssl-1.1.1g-he774522_0
pip pkgs/main/win-64::pip-20.1.1-py37_1
python pkgs/main/win-64::python-3.7.7-h81c818b_4
setuptools pkgs/main/win-64::setuptools-49.2.0-py37_0
sqlite pkgs/main/win-64::sqlite-3.32.3-h2a8f88b_0
vc pkgs/main/win-64::vc-14.1-h0510ff6_4
vs2015_runtime pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_3
wheel pkgs/main/win-64::wheel-0.34.2-py37_0
wincertstore pkgs/main/win-64::wincertstore-0.2-py37_0
zlib pkgs/main/win-64::zlib-1.2.11-h62dcd97_4
Proceed ([y]/n)? y
Downloading and Extracting Packages
vs2015_runtime-14.16 | 1.2 MB | ############################################################################ | 100%
python-3.7.7 | 14.3 MB | ############################################################################ | 100%
setuptools-49.2.0 | 760 KB | ############################################################################ | 100%
sqlite-3.32.3 | 802 KB | ############################################################################ | 100%
certifi-2020.6.20 | 156 KB | ############################################################################ | 100%
openssl-1.1.1g | 4.8 MB | ############################################################################ | 100%
wheel-0.34.2 | 66 KB | ############################################################################ | 100%
ca-certificates-2020 | 125 KB | ############################################################################ | 100%
zlib-1.2.11 | 113 KB | ############################################################################ | 100%
pip-20.1.1 | 1.7 MB | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate frog_test
#
# To deactivate an active environment, use
#
# $ conda deactivate
关注最末尾这几句话:
# To activate this environment, use
#
# $ conda activate frog_test
#
# To deactivate an active environment, use
#
# $ conda deactivate
这两句告诉我们:
conda activate frog_test # 进入创建的虚拟环境
conda deactivate # 退出虚拟环境
查看是否创建成功
C:\Users\Administrator>conda env list
# conda environments:
#
base * D:\r\Anaconda3
frog_test D:\r\Anaconda3\envs\frog_test # 创建成功
professior D:\r\Anaconda3\envs\professior
pycharm book D:\r\Anaconda3\envs\pycharm book
3、进入环境
C:\Users\Administrator>conda activate frog_test
(frog_test) C:\Users\Administrator>
看到(frog_test)
则代表进入成功
4、退出环境
(frog_test) C:\Users\Administrator>conda deactivate
C:\Users\Administrator>
5、相关模块包
# 指定环境,查看已安装的包
conda list -n frog_test
以安装Pandas为例
# 指定环境,安装指定版本的包
conda install -n frog_test pandas==1.0.4
# 指定环境,更新包
conda update -n frog_test pandas
# 指定环境,删除包
conda remove -n frog_test pandas
指定环境,查看已安装的包conda list -n frog_test
C:\Users\Administrator>conda list -n frog_test
# packages in environment at D:\r\Anaconda3\envs\frog_test:
#
# Name Version Build Channel
ca-certificates 2020.6.24 0 defaults
certifi 2020.6.20 py37_0 defaults
openssl 1.1.1g he774522_0 defaults
pip 20.1.1 py37_1 defaults
python 3.7.7 h81c818b_4 defaults
setuptools 49.2.0 py37_0 defaults
sqlite 3.32.3 h2a8f88b_0 defaults
vc 14.1 h0510ff6_4 defaults
vs2015_runtime 14.16.27012 hf0eaf9b_3 defaults
wheel 0.34.2 py37_0 defaults
wincertstore 0.2 py37_0 defaults
zlib 1.2.11 h62dcd97_4 defaults
指定环境,安装指定版本的包conda install -n frog_test pandas==1.0.4
C:\Users\Administrator>conda install -n frog_test pandas==1.0.4
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: done
==> WARNING: A newer version of conda exists. <==
current version: 4.7.12
latest version: 4.8.3
Please update conda by running
$ conda update -n base -c defaults conda
## Package Plan ##
environment location: D:\r\Anaconda3\envs\frog_test
added / updated specs:
- pandas==1.0.4
The following packages will be downloaded:
package | build
---------------------------|-----------------
blas-1.0 | mkl 6 KB https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
intel-openmp-2020.1 | 216 1.6 MB defaults
mkl-2020.1 | 216 99.3 MB defaults
mkl_fft-1.1.0 | py37h45dec08_0 116 KB defaults
mkl_random-1.1.1 | py37h47e9c7a_0 233 KB defaults
numpy-1.18.5 | py37h6530119_0 5 KB defaults
numpy-base-1.18.5 | py37hc3f5095_0 3.8 MB defaults
pandas-1.0.4 | py37h47e9c7a_0 7.5 MB defaults
python-dateutil-2.8.1 | py_0 215 KB defaults
pytz-2020.1 | py_0 184 KB defaults
six-1.15.0 | py_0 13 KB defaults
------------------------------------------------------------
Total: 112.9 MB
The following NEW packages will be INSTALLED:
blas anaconda/pkgs/free/win-64::blas-1.0-mkl
icc_rt pkgs/main/win-64::icc_rt-2019.0.0-h0cc432a_1
intel-openmp pkgs/main/win-64::intel-openmp-2020.1-216
mkl pkgs/main/win-64::mkl-2020.1-216
mkl-service pkgs/main/win-64::mkl-service-2.3.0-py37hb782905_0
mkl_fft pkgs/main/win-64::mkl_fft-1.1.0-py37h45dec08_0
mkl_random pkgs/main/win-64::mkl_random-1.1.1-py37h47e9c7a_0
numpy pkgs/main/win-64::numpy-1.18.5-py37h6530119_0
numpy-base pkgs/main/win-64::numpy-base-1.18.5-py37hc3f5095_0
pandas pkgs/main/win-64::pandas-1.0.4-py37h47e9c7a_0
python-dateutil pkgs/main/noarch::python-dateutil-2.8.1-py_0
pytz pkgs/main/noarch::pytz-2020.1-py_0
six pkgs/main/noarch::six-1.15.0-py_0
Proceed ([y]/n)? y
Downloading and Extracting Packages
pytz-2020.1 | 184 KB | ############################################################################ | 100%
python-dateutil-2.8. | 215 KB | ############################################################################ | 100%
pandas-1.0.4 | 7.5 MB | ############################################################################ | 100%
mkl-2020.1 | 99.3 MB | ############5 | 16%
安装慢的话可以用镜像源,进入虚拟环境后用
pip install 模块名 -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
# 进入虚拟环境
C:\Users\Administrator>conda activate frog_test
# 查看模块包
(frog_test) C:\Users\Administrator>pip list
Package Version
----------------- -------------------
astroid 2.3.3
certifi 2020.6.20
colorama 0.4.3
isort 4.3.21
lazy-object-proxy 1.4.3
mccabe 0.6.1
pip 20.1.1
pylint 2.4.4
setuptools 49.2.0.post20200714
six 1.14.0
typed-ast 1.4.1
wheel 0.34.2
wincertstore 0.2
wrapt 1.11.2
# 安装模块包
(frog_test) C:\Users\Administrator>pip install pandas==1.0.4 -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
Looking in indexes: http://mirrors.aliyun.com/pypi/simple
Collecting pandas==1.0.4
Downloading http://mirrors.aliyun.com/pypi/packages/1d/eb/b4f68f54ad287d583c9c3b3c77f865615f832f092810f20d2b44498cd06c/pandas-1.0.4-cp37-cp37m-win_amd64.whl (8.7 MB)
|████████████████████████████████| 8.7 MB 3.3 MB/s
Collecting python-dateutil>=2.6.1
Downloading http://mirrors.aliyun.com/pypi/packages/d4/70/d60450c3dd48ef87586924207ae8907090de0b306af2bce5d134d78615cb/python_dateutil-2.8.1-py2.py3-none-any.whl (227 kB)
|████████████████████████████████| 227 kB 6.4 MB/s
Collecting pytz>=2017.2
Downloading http://mirrors.aliyun.com/pypi/packages/4f/a4/879454d49688e2fad93e59d7d4efda580b783c745fd2ec2a3adf87b0808d/pytz-2020.1-py2.py3-none-any.whl (510 kB)
|████████████████████████████████| 510 kB 6.4 MB/s
Collecting numpy>=1.13.3
Downloading http://mirrors.aliyun.com/pypi/packages/df/cb/7af5de0a4068ce314e13074a4f86afada7fa86a095489bbc80f5dabd4156/numpy-1.19.1-cp37-cp37m-win_amd64.whl (12.9 MB)
|████████████████████████████████| 12.9 MB 2.2 MB/s
Requirement already satisfied: six>=1.5 in c:\users\administrator\appdata\roaming\python\python37\site-packages (from python-dateutil>=2.6.1->pandas==1.0.4) (1.14.0)
Installing collected packages: python-dateutil, pytz, numpy, pandas
Successfully installed numpy-1.19.1 pandas-1.0.4 python-dateutil-2.8.1 pytz-2020.1
再次查看,可以发现已经安装成功。
其余如:更新模块包、删除模块包,全部都可以进入虚拟环境后 pip 进行
# 更新模块包
pip install --upgrade 要升级的包名
# 删除模块包
pip uninstall 模块包
更新pandas模块包
更新pandas模块包pip install --upgrade pandas -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
(frog_test) C:\Users\Administrator>pip install --upgrade pandas -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
Looking in indexes: http://mirrors.aliyun.com/pypi/simple
Collecting pandas
Downloading http://mirrors.aliyun.com/pypi/packages/da/90/44d5e0a5d42506d2e31544c8f44c54be88c2128f6fc482c01de29ecfb365/pandas-1.0.5-cp37-cp37m-win_amd64.whl (8.7 MB)
|████████████████████████████████| 8.7 MB 6.4 MB/s
Requirement already satisfied, skipping upgrade: pytz>=2017.2 in d:\r\anaconda3\envs\frog_test\lib\site-packages (from pandas) (2020.1)
Requirement already satisfied, skipping upgrade: numpy>=1.13.3 in d:\r\anaconda3\envs\frog_test\lib\site-packages (from pandas) (1.19.1)
Requirement already satisfied, skipping upgrade: python-dateutil>=2.6.1 in d:\r\anaconda3\envs\frog_test\lib\site-packages (from pandas) (2.8.1)
Requirement already satisfied, skipping upgrade: six>=1.5 in c:\users\administrator\appdata\roaming\python\python37\site-packages (from python-dateutil>=2.6.1->pandas) (1.14.0)
Installing collected packages: pandas
Attempting uninstall: pandas
Found existing installation: pandas 1.0.4
Uninstalling pandas-1.0.4:
Successfully uninstalled pandas-1.0.4
Successfully installed pandas-1.0.5
之前安装的1.0.4版本pandas变成1.0.5了。
删除pandas模块包
(frog_test) C:\Users\Administrator>pip uninstall pandas
Found existing installation: pandas 1.0.5
Uninstalling pandas-1.0.5:
Would remove:
d:\r\anaconda3\envs\frog_test\lib\site-packages\pandas-1.0.5.dist-info\*
d:\r\anaconda3\envs\frog_test\lib\site-packages\pandas\*
Proceed (y/n)? y
Successfully uninstalled pandas-1.0.5
(frog_test) C:\Users\Administrator>pip list
Package Version
----------------- -------------------
astroid 2.3.3
certifi 2020.6.20
colorama 0.4.3
isort 4.3.21
lazy-object-proxy 1.4.3
mccabe 0.6.1
numpy 1.19.1
pip 20.1.1
pylint 2.4.4
python-dateutil 2.8.1
pytz 2020.1
setuptools 49.2.0.post20200714
six 1.14.0
typed-ast 1.4.1
wheel 0.34.2
wincertstore 0.2
wrapt 1.11.2
删除成功
=====================================================