Python三神器之virtualenv&virtualenvwrapper

来源:互联网 发布:淘宝卖东西被骗 编辑:程序博客网 时间:2024/05/20 11:48

virtualenv是一个用于创建独立Python环境的工具,可以解决多项目之间依赖库版本不一致的问题,每个虚拟环境都是一个独立的Python环境,可以安装只属于这个环境的库。

virtualenvwrapper是virtualenv的一个扩展工具,它可以是我们在使用、管理虚拟环境时更简便、易用。

一. 安装

1. 安装virtualenv

[root@localhost bin]# pip3 install virtualenvCollecting virtualenv  Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)    100% |████████████████████████████████| 1.8MB 770kB/sInstalling collected packages: virtualenvSuccessfully installed virtualenv-15.1.0[root@localhost bin]# ln -s /usr/local/python3/bin/virtualenv /usr/local/bin/virtualenv#创建virtualenv的软连接[root@localhost ~]# ln -s /usr/local/python3/bin/virtualenv-clone /usr/local/bin/virtualenv-clone #创建virtualenv-clone的软连接[root@localhost ~]#

在系统目录/usr/local/bin下创建virtualenv和virtualenv-clone的软连接,是因为virtualenvwrapper会使用在(/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)下的virtualenv和virtualenv-clone,所以必须保证virtualenv和virtualenv-clone的存在。

2. 安装virtualenvwrapper

[root@localhost ~]# pip3 install virtualenvwrapperCollecting virtualenvwrapper  Downloading virtualenvwrapper-4.8.1-py2.py3-none-any.whlCollecting virtualenv-clone (from virtualenvwrapper)  Downloading virtualenv-clone-0.2.6.tar.gzCollecting stevedore (from virtualenvwrapper)  Downloading stevedore-1.26.0-py2.py3-none-any.whlRequirement already satisfied: virtualenv in /usr/local/python3/lib/python3.6/site-packages (from virtualenvwrapper)Collecting pbr!=2.1.0,>=2.0.0 (from stevedore->virtualenvwrapper)  Downloading pbr-3.1.1-py2.py3-none-any.whl (99kB)    100% |████████████████████████████████| 102kB 497kB/sCollecting six>=1.9.0 (from stevedore->virtualenvwrapper)  Downloading six-1.10.0-py2.py3-none-any.whlInstalling collected packages: virtualenv-clone, pbr, six, stevedore, virtualenvwrapper  Running setup.py install for virtualenv-clone ... doneSuccessfully installed pbr-3.1.1 six-1.10.0 stevedore-1.26.0 virtualenv-clone-0.2.6 virtualenvwrapper-4.8.1[root@localhost ~]#

我们通常都是通过virtualenvwrapper去使用、管理我们的虚拟环境,所以这里只介绍virtualenvwrapper的用法。

二. 使用

1. 初始化

1.1. shell初始化(单次有效,shell关闭后需重新初始化)

[root@localhost ~]# export WORKON_HOME=~/Envs#设置环境变量WORKON_HOME[root@localhost ~]# mkdir -p $WORKON_HOME#创建环境变量WORKON_HOME指定的目录[root@localhost ~]# echo $WORKON_HOME#显示环境变量WORKON_HOME的值/root/Envs[root@localhost ~]# export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3#设置环境变量VIRTUALENVWRAPPER_PYTHON[root@localhost ~]# echo $VIRTUALENVWRAPPER_PYTHON#显示环境变量VIRTUALENVWRAPPER_PYTHON的值/usr/bin/python3[root@localhost ~]# source /usr/local/python3/bin/virtualenvwrapper.shvirtualenvwrapper.user_scripts creating /root/Envs/premkprojectvirtualenvwrapper.user_scripts creating /root/Envs/postmkprojectvirtualenvwrapper.user_scripts creating /root/Envs/initializevirtualenvwrapper.user_scripts creating /root/Envs/premkvirtualenvvirtualenvwrapper.user_scripts creating /root/Envs/postmkvirtualenvvirtualenvwrapper.user_scripts creating /root/Envs/prermvirtualenvvirtualenvwrapper.user_scripts creating /root/Envs/postrmvirtualenvvirtualenvwrapper.user_scripts creating /root/Envs/predeactivatevirtualenvwrapper.user_scripts creating /root/Envs/postdeactivatevirtualenvwrapper.user_scripts creating /root/Envs/preactivatevirtualenvwrapper.user_scripts creating /root/Envs/postactivatevirtualenvwrapper.user_scripts creating /root/Envs/get_env_details[root@localhost ~]#

其中设置环境变量VIRTUALENVWRAPPER_PYTHON是因为我的系统安装了两个版本的Python,默认路径/usr/bin/python是Python2的目录,而virtualenvwrapper我是用Python3安装的,所以要手动讲默认的VIRTUALENVWRAPPER_PYTHON变量修改成Python3的目录,如果是使用默认版本的Python安装的virtualenvwrapper可以省略此步骤。

1.2. shell启动文件初始化(永久有效,shell启动时自动初始化)

在~/.bashrc文件最后添加以下语句,保存后shell下执行source ~/.bashrc。~/.bashrc文件是当前用户的shell启动配置文件,如果想要修改全局的shell,可以修改/etc/bashrc文件。

export WORKON_HOME=$HOME/.virtualenvsexport VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3export PROJECT_HOME=$HOME/Develsource /usr/local/python3/bin/virtualenvwrapper.sh

2. 新建虚拟环境

[root@localhost ~]# mkvirtualenv env1Using base prefix '/usr/local/python3'New python executable in /root/.virtualenvs/env1/bin/python3.6Also creating executable in /root/.virtualenvs/env1/bin/pythonInstalling setuptools, pip, wheel...done.virtualenvwrapper.user_scripts creating /root/.virtualenvs/env1/bin/predeactivatevirtualenvwrapper.user_scripts creating /root/.virtualenvs/env1/bin/postdeactivatevirtualenvwrapper.user_scripts creating /root/.virtualenvs/env1/bin/preactivatevirtualenvwrapper.user_scripts creating /root/.virtualenvs/env1/bin/postactivatevirtualenvwrapper.user_scripts creating /root/.virtualenvs/env1/bin/get_env_details(env1) [root@localhost etc]# workonenv1(env1) [root@localhost etc]#

3. 新建临时的虚拟环境(退出环境后自动删除)

[root@localhost ~]# mktmpenvUsing base prefix '/usr/local/python3'New python executable in /root/.virtualenvs/tmp-46e87484b9f3cfc/bin/python3.6Also creating executable in /root/.virtualenvs/tmp-46e87484b9f3cfc/bin/pythonInstalling setuptools, pip, wheel...done.virtualenvwrapper.user_scripts creating /root/.virtualenvs/tmp-46e87484b9f3cfc/bin/predeactivatevirtualenvwrapper.user_scripts creating /root/.virtualenvs/tmp-46e87484b9f3cfc/bin/postdeactivatevirtualenvwrapper.user_scripts creating /root/.virtualenvs/tmp-46e87484b9f3cfc/bin/preactivatevirtualenvwrapper.user_scripts creating /root/.virtualenvs/tmp-46e87484b9f3cfc/bin/postactivatevirtualenvwrapper.user_scripts creating /root/.virtualenvs/tmp-46e87484b9f3cfc/bin/get_env_detailsThis is a temporary environment. It will be deleted when you run 'deactivate'.(tmp-46e87484b9f3cfc) [root@localhost tmp-46e87484b9f3cfc]#

4. 列出所有环境

[root@localhost ~]# lsvirtualenvenv1====tmp-46e87484b9f3cfc===================[root@localhost ~]#

5. 查看指定环境的详情

[root@localhost ~]# showvirtualenv env1[root@localhost ~]#

6. 删除指定环境

[root@localhost ~]# rmvirtualenv env1Removing env1...[root@localhost ~]#

删除环境之前必须deactivate待删除的环境。mktmpenv创建的临时虚拟环境,不需要手动删除,在deactivate后其会自动被删除。

7. 复制虚拟环境(支持得不是很好,谨慎使用)

[root@localhost ~]# cpvirtualenv env1 env2 #使用到virtualenv-cloneCopying env1 as env2...(env2) [root@localhost ~]#

8. 所有虚拟环境下执行指定命令

[root@localhost ~]# allvirtualenv pip install -U pipenv1====Requirement already up-to-date: pip in ./lib/python3.6/site-packagesenv2====Requirement already up-to-date: pip in ./lib/python3.6/site-packages[root@localhost ~]#

9. 列出所有虚拟环境或切换虚拟环境

[root@localhost ~]# workon#列出所有虚拟环境env1env2[root@localhost ~]# workon env1#切换至虚拟环境env1(env1) [root@localhost ~]#

10. 从当前虚拟环境中退回到系统环境(使用系统中的python)

(env1) [root@localhost ~]# deactivate[root@localhost ~]#

11. 改变当前的工作目录至当前虚拟环境$VIRTUAL_ENV的指定目录或其子目录

(env1) [root@localhost ~]# pwd/root(env1) [root@localhost ~]# cdvirtualenv(env1) [root@localhost env1]# pwd/root/.virtualenvs/env1(env1) [root@localhost env1]# cd ~(env1) [root@localhost ~]# cdvirtualenv bin(env1) [root@localhost bin]# pwd/root/.virtualenvs/env1/bin(env1) [root@localhost bin]#

12. 改变当前的工作目录至当前虚拟环境$VIRTUAL_ENV下的site-packages目录或site-packages的子目录

(env1) [root@localhost ~]# pwd/root(env1) [root@localhost ~]# cdsitepackages(env1) [root@localhost site-packages]# pwd/root/.virtualenvs/env1/lib/python3.6/site-packages(env1) [root@localhost site-packages]# cd ~(env1) [root@localhost ~]# cdsitepackages pip(env1) [root@localhost pip]# pwd/root/.virtualenvs/env1/lib/python3.6/site-packages/pip(env1) [root@localhost pip]#

13. 列出当前的虚拟环境site-packages目录下的内容

(env1) [root@localhost ~]# lssitepackageseasy_install.py  pip-9.0.1.dist-info  __pycache__  setuptools-36.4.0.dist-info  wheel-0.30.0.dist-infopip              pkg_resources        setuptools   wheel(env1) [root@localhost ~]# cdsitepackages(env1) [root@localhost site-packages]# pwd/root/.virtualenvs/env1/lib/python3.6/site-packages(env1) [root@localhost site-packages]# lseasy_install.py  pip-9.0.1.dist-info  __pycache__  setuptools-36.4.0.dist-info  wheel-0.30.0.dist-infopip              pkg_resources        setuptools   wheel(env1) [root@localhost site-packages]#













原创粉丝点击