◎知识点
virtualenvwrapper概述
virtualenvwrapper安装
virtualenvwrapper使用
1.virtualenvwrapper概述
virtualenv虽然解决了Python第三方库版本冲突、管理混乱等问题,但是在使用过程中不方便之处也很明显。比如:
虚拟环境管理不方便:虚拟环境分散在各个路径下,项目一多时间一长,管理难免混乱,不知道在何处,是何版本
虚拟环境激活不方便:当要使用某个虚拟环境时,需手动先切换到该虚拟环境的Scripts目录或bin目录,然后执行activate或source activate
虚拟环境切换也不方便:当切换到另一虚拟环境时,需先反激活当前的虚拟环境,然后再进入对应目录,执行激活命令
第三方库virtualenvwrapper正是为了解决virtualenv在使用过程中的诸多不便而产生的,从 Pypi virtualenvwrapper 官方描述中也可以看到详细介绍:
virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.
Features
Organizes all of your virtual environments in one place. 在同一个路径下管理所有的虚拟环境
Wrappers for creating, copying and deleting environments, including user-configurable hooks. 在创建、拷贝和删除虚拟环境时都进行了封装,包括用户可配置的钩子
Use a single command to switch between environments. 只使用一个命令就可以在多个虚拟环境中进行切换
Tab completion for commands that take a virtual environment as argument. 对于使用一个虚拟环境作为参数的命令,提供了Tab补全的功能
User-configurable hooks for all operations. 对于所有操作都提供了用户可配置的钩子
Plugin system for more creating sharable extensions. 提供插件系统用于更好地创建可共享的扩展
2.virtualenvwrapper安装
# 安装virtualenvwrapper ➜ ~ pip3 install virtualenvwrapper # 在~/.bash_profile中添加两行 export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 # 指定virtualenvwrapper所对应的Python安装目录中的Python解释器 source ~/Library/Python/3.8/bin/virtualenvwrapper.sh # 执行virtualenvwrapper所对应的Python安装目录中的virtualenvwrapper.sh # 执行virtualenvwrapper ➜ ~ virtualenvwrapper virtualenvwrapper is a set of extensions to Ian Bicking's virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies. For more information please refer to the documentation: http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html Commands available: add2virtualenv: add directory to the import path allvirtualenv: run a command in all virtualenvs cdproject: change directory to the active project cdsitepackages: change to the site-packages directory cdvirtualenv: change to the $VIRTUAL_ENV directory cpvirtualenv: duplicate the named virtualenv to make a new one lssitepackages: list contents of the site-packages directory lsvirtualenv: list virtualenvs mkproject: create a new project directory and its associated virtualenv mktmpenv: create a temporary virtualenv mkvirtualenv: Create a new virtualenv in $WORKON_HOME rmvirtualenv: Remove a virtualenv setvirtualenvproject: associate a project directory with a virtualenv showvirtualenv: show details of a single virtualenv toggleglobalsitepackages: turn access to global site-packages on/off virtualenvwrapper: show this help message wipeenv: remove all packages installed in the current virtualenv workon: list or change working virtualenvs
3.virtualenvwrapper使用
3.1 创建虚拟环境
➜ ~ mkvirtualenv MyVENV1 created virtual environment CPython3.9.10.final.0-64 in 391ms creator CPython3Posix(dest=/Users/anzhihe/.virtualenvs/MyVENV1, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/anzhihe/Library/Application Support/virtualenv) added seed packages: pip==23.0.1, setuptools==67.4.0, wheel==0.38.4 activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV1/bin/predeactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV1/bin/postdeactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV1/bin/preactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV1/bin/postactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV1/bin/get_env_details (MyVENV1) ➜ ~ mkvirtualenv MyVENV2 created virtual environment CPython3.9.10.final.0-64 in 373ms creator CPython3Posix(dest=/Users/anzhihe/.virtualenvs/MyVENV2, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/anzhihe/Library/Application Support/virtualenv) added seed packages: pip==23.0.1, setuptools==67.4.0, wheel==0.38.4 activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV2/bin/predeactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV2/bin/postdeactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV2/bin/preactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV2/bin/postactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/.virtualenvs/MyVENV2/bin/get_env_details (MyVENV2) ➜ ~ deactivate # 修改WORKON_HOME默认路径 ➜ ~ mkdir -p ~/MyVirEnvs ➜ ~ export WORKON_HOME=~/MyVirEnvs ➜ ~ mkvirtualenv MyVENV3 created virtual environment CPython3.9.10.final.0-64 in 365ms creator CPython3Posix(dest=/Users/anzhihe/MyVirEnvs/MyVENV3, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/anzhihe/Library/Application Support/virtualenv) added seed packages: pip==23.0.1, setuptools==67.4.0, wheel==0.38.4 activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV3/bin/predeactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV3/bin/postdeactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV3/bin/preactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV3/bin/postactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV3/bin/get_env_details (MyVENV3) ➜ ~ where python2 python2: aliased to /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Library/Frameworks/Python.framework/Versions/2.7/bin/python2 /usr/local/bin/python2 (MyVENV3) ➜ ~ mkvirtualenv -p "/Library/Frameworks/Python.framework/Versions/2.7/bin/python2" MyVENV4 created virtual environment CPython2.7.18.final.0-64 in 850ms creator CPython2macOsFramework(dest=/Users/anzhihe/MyVirEnvs/MyVENV4, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/anzhihe/Library/Application Support/virtualenv) added seed packages: pip==20.3.4, setuptools==44.1.1, wheel==0.37.1 activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV4/bin/predeactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV4/bin/postdeactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV4/bin/preactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV4/bin/postactivate virtualenvwrapper.user_scripts creating /Users/anzhihe/MyVirEnvs/MyVENV4/bin/get_env_details (MyVENV4) ➜ ~
3.2 激活或切换
➜ ~ lsvirtualenv MyVENV3 ======= MyVENV4 ======= ➜ ~ workon MyVENV3 MyVENV4 ➜ ~ workon MyVENV3 (MyVENV3) ➜ ~ workon MyVENV4 (MyVENV4) ➜ ~
3.3 虚拟环境操作
(MyVENV4) ➜ ~ workon MyVENV3 (MyVENV3) ➜ ~ where python /Users/anzhihe/MyVirEnvs/MyVENV3/bin/python /Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/local/bin/python (MyVENV3) ➜ ~ where pip /Users/anzhihe/MyVirEnvs/MyVENV3/bin/pip /Library/Frameworks/Python.framework/Versions/2.7/bin/pip /usr/local/bin/pip (MyVENV3) ➜ ~ pip --version pip 23.0.1 from /Users/anzhihe/MyVirEnvs/MyVENV3/lib/python3.9/site-packages/pip (python 3.9) (MyVENV3) ➜ ~ pip install requests (MyVENV3) ➜ ~ cat test.py import requests print(requests) (MyVENV3) ➜ ~ python test.py <module 'requests' from '/Users/anzhihe/MyVirEnvs/MyVENV3/lib/python3.9/site-packages/requests/__init__.py'> (MyVENV3) ➜ ~ cdvirtualenv (MyVENV3) ➜ MyVENV3 cdsitepackages (MyVENV3) ➜ site-packages lssitepackages __pycache__ idna setuptools-67.4.0.dist-info _distutils_hack idna-3.4.dist-info setuptools-67.4.0.virtualenv _virtualenv.pth pip urllib3 _virtualenv.py pip-23.0.1.dist-info urllib3-1.26.15.dist-info certifi pip-23.0.1.virtualenv wheel certifi-2022.12.7.dist-info pkg_resources wheel-0.38.4.dist-info charset_normalizer requests wheel-0.38.4.virtualenv charset_normalizer-3.1.0.dist-info requests-2.28.2.dist-info distutils-precedence.pth setuptools
3.4 反激活虚拟环境
(MyVENV3) ➜ site-packages (MyVENV3) ➜ site-packages deactivate ➜ site-packages rmvirtualenv MyVENV3 Removing MyVENV3... ➜ MyVirEnvs