Python自带的那个环境是系统环境,同一个项目的不同版本可能会依赖不同版本的依赖包,如果都放在系统环境下会使系统环境变得很庞大,同时操作起来也不太方便,如果给每个项目都单独配置一个环境,各个项目之间互不干扰,开发起来就方便些,每个项目单独的环境叫做虚拟环境。
virtualenv 安装虚拟环境工具
pip3 install virtualenv
1.创建一个虚拟环境
C:\Users\11622>cd D:\Python learning
C:\Users\11622>d:
D:\Python learning>virtualenv venv_test
created virtual environment in 4361ms CPython3Windows(dest=D:\Python learning\venv_test, clear=False, global=False) with seeder FromAppData pip=latest setuptools=latest wheel=latest app_data_dir=C:\Users\11622\AppData\Local\pypa\virtualenv\seed-v1 via=copy
2.进入到虚拟环境 windows使用虚拟环境script目录下的activate命令,linux环境使用source + activat目录来进入到虚拟环境。
D:\Python learning>cd venv_test
D:\Python learning\venv_test>cd Scripts
D:\Python learning\venv_test\Scripts>activate
D:\Python learning\venv_test\Scripts>pip3 install numpy
Collecting numpy
Downloading numpy-1.18.1-cp38-cp38-win_amd64.whl (12.8 MB)
|████████████████████████████████| 12.8 MB 72 kB/s
Installing collected packages: numpy
Successfully installed numpy-1.18.1
D:\Python learning\venv_test\Scripts>pip3 list
Package Version
---------- -------
numpy 1.18.1
pip 20.0.2
setuptools 45.2.0
wheel 0.34.2
3.退出虚拟环境 进入到虚拟环境后在任何虚拟环境执行deactivate都可以退出当前虚拟环境。
D:\Python learning\venv_test\Scripts>deactivate
D:\Python learning\venv_test\Scripts>cd ..
D:\Python learning\venv_test>pip3 list
Package Version
---------------------- -------
aliyun-python-sdk-core 2.13.14
appdirs 1.4.3
distlib 0.3.0
filelock 3.0.12
jmespath 0.9.4
pip 19.2.3
setuptools 41.2.0
six 1.14.0
virtualenv 20.0.4
WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
virtualenvwrapper 使用virtualenv必须到虚拟环境的scripts目录下进入虚拟环境,使用起来不算方便,于是有了一款virtualenvwrapper工具来管理虚拟环境。
安装virtualenvwrapper
pip3 install virtualenvwrapper-win
1.创建一个虚拟环境 mkvirtualenv
D:\Python learning>mkvirtualenv test_env2
C:\Users\11622\Envs is not a directory, creating
created virtual environment in 389ms CPython3Windows(dest=C:\Users\11622\Envs\test_env2, clear=False, global=False) with seeder FromAppData pip=latest setuptools=latest wheel=latest app_data_dir=C:\Users\11622\AppData\Local\pypa\virtualenv\seed-v1 via=copy
virtualenvwrapper会默认在当前用户下的Envs目录下创建虚拟环境。
2.列出所有虚拟环境 lsvirtualenv
D:\Python learning>lsvirtualenv
dir /b /ad "C:\Users\11622\Envs"
==============================================================================
test_env2
3.进入到某一个虚拟环境 workon
D:\Python learning>workon test_env2
D:\Python learning>pip3 list
Package Version
---------- -------
pip 20.0.2
setuptools 45.2.0
wheel 0.34.2
D:\Python learning>pip3 install pandas
4.退出当前env deactivate
D:\Python learning>deactivate
5.删除某一个env rmvirtualenv 如删除的env的lib目录不是空的,删除操作需要执行两次。
D:\Python learning>rmvirtualenv test_env2
test_env2\Lib - 目录不是空的。
Deleted C:\Users\11622\Envs\test_env2
D:\Python learning>lsvirtualenv
dir /b /ad "C:\Users\11622\Envs"
==============================================================================
test_env2
D:\Python learning>rmvirtualenv test_env2
Deleted C:\Users\11622\Envs\test_env2
6.进入到某个env所在的目录 cdvirtualenv
D:\Python learning>mkvirtualenv test_env3
created virtual environment in 378ms CPython3Windows(dest=C:\Users\11622\Envs\test_env3, clear=False, global=False) with seeder FromAppData pip=latest setuptools=latest wheel=latest app_data_dir=C:\Users\11622\AppData\Local\pypa\virtualenv\seed-v1 via=copy
D:\Python learning>cdvirtualenv test_env3
C:\Users\11622\Envs\test_env3>
本人是做大数据开发的,在微信上开了个个人号,会经常在上面分享一些学习心得,原创文章都会首发到公众号上,感兴趣的盆友可以关注下哦!