前言
之前一篇文章分享了从Python 2.6 升级至 Python 2.7,在开发环境中我们也和很多朋友一样使用virtualenv和virtualenvwrapper来管理和解决不同用户和应用之间的冲突,比如一个DevOps团队在Linux环境中协作开发,每个用户都需要搭建一套Django+Ansible
virtualenv为应用提供了隔离的Python运行环境,解决了不同应用间多版本的冲突问题
更新历史
2018年04月11日 - 初稿
阅读原文 - https://wsgzao.github.io/post/virtualenv/
扩展阅读
PyPI - https://pypi.python.org/pypi/virtualenv
virtualenv - http://pythonguidecn.readthedocs.io/zh/latest/dev/virtualenvs.html
virtualenvwrapper - http://virtualenvwrapper.readthedocs.io/en/latest/index.html
virtualenv
virtualenv 是一个创建隔绝的Python环境的工具。virtualenv创建一个包含所有必要的可执行文件的文件夹,用来使用Python工程所需的包。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| python -V Python 2.7.13
pip install virtualenv
su - wangao
[wangao@localhost ~]$ virtualenv venv New python executable in /home/wangao/venv/bin/python Installing setuptools, pip, wheel...done.
[wangao@localhost ~]$ source venv/bin/activate (venv) [wangao@localhost ~]$ pip install requests
(venv) [wangao@localhost ~]$ deactivate
rm -rf venv
|
virtualenvwrapper
virtualenvwrapper 提供了一系列命令使得和虚拟环境工作变得愉快许多。它把你所有的虚拟环境都放在一个地方。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| pip install virtualenvwrapper
[wangao@localhost ~]$ vim .bash_profile
export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
export PIP_REQUIRE_VIRTUALENV=true
[wangao@localhost ~]$ source .bash_profile virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/premkproject virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/postmkproject virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/initialize virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/premkvirtualenv virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/postmkvirtualenv virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/prermvirtualenv virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/postrmvirtualenv virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/predeactivate virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/postdeactivate virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/preactivate virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/postactivate virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/get_env_details
su - wangao
[wangao@localhost ~]$ mkvirtualenv venv New python executable in /home/wangao/.virtualenvs/venv/bin/python Installing setuptools, pip, wheel...done. virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/venv/bin/predeactivate virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/venv/bin/postdeactivate virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/venv/bin/preactivate virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/venv/bin/postactivate virtualenvwrapper.user_scripts creating /home/wangao/.virtualenvs/venv/bin/get_env_details
(venv) [wangao@localhost ~]$ workon venv
(venv) [wangao@localhost ~]$ deactivate [wangao@localhost ~]$
[wangao@localhost ~]$ rmvirtualenv venv
|