6

使用 virtualenv 和 virtualenvwrapper 管理多版本 Python 开发环境

 3 years ago
source link: https://wsgzao.github.io/post/virtualenv/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
使用 virtualenv 和 virtualenvwrapper 管理多版本 Python 开发环境

之前一篇文章分享了从 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 工程所需的包。

# 检查 python 版本是否升级至 2.7 以上 
python -V
Python 2.7.13

# 安装 virtualenv
pip install virtualenv
# 切换用户创建一个虚拟环境
su - wangao
# virtualenv -p /usr/bin/python2.7 venv
# virtualenv --no-site-packages venv
[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 提供了一系列命令使得和虚拟环境工作变得愉快许多。它把你所有的虚拟环境都放在一个地方。

# 推荐直接使用 virtualenvwrapper 来管理 python 虚拟环境 
pip install virtualenvwrapper

# 安装好之后需要简单配置下,主要是设置独立环境的保存路径,推荐在. bash_profile 下追加
[wangao@localhost ~]$ vim .bash_profile

# 建议放入. bash_profile
export WORKON_HOME=~/.virtualenvs
# 不同系统 virtualenvwrapper.sh 存放的位置可能不同
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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK