python虚拟环境virtualenvwrapper

上一篇:python虚拟环境virtualenv
http://www.jianshu.com/p/e17263be54ea

virtualenv 的一个最大的缺点就是,每次开启虚拟环境之前要去虚拟环境所在目录下的 bin 目录下 source 一下 activate,这就需要我们记住每个虚拟环境所在的目录。

Virtaulenvwrapper是virtualenv的扩展包,用于更方便管理虚拟环境,它可以做:

  • 将所有虚拟环境组织在一个目录下
  • 管理(新增,删除,复制)虚拟环境
  • 更方便的在不同的虚拟环境下进行切换
  • 用户可以为所有的命令操作自定义hooks (详见官网 Per-User Customization)

一、安装及配置Virtaulenvwrapper

1、安装virtualenvwrapper

pip install virtualenvwrapper

2、配置 virtualenvwrapper

它需要指定一个环境变量,叫做 WORKON_HOME,并且需要运行一下它的初始化工具virtualenvwrapper.sh,这个脚本在/usr/local/bin/ 目录下。

WORKON_HOME 就是它将要用来存放各种虚拟环境目录的目录,这里可以设置为~/.virtualenvs

$ export WORKON_HOME='~/.virtualenvs'
$ source /usr/local/bin/virtualenvwrapper.sh

由于每次都需要执行这两步操作,所以可以将其写入终端的配置文件中。例如,如果使用 bash,则添加到~/.bashrc 中;如果使用zsh,则添加到 ~/.zshrc 中。这样每次启动终端的时候都会自动运行,终端启动之后就可以用 virtualenvwrapper

二、使用virtualenvwrapper

1、mkvirtualenv [-a project_path] [-i package] [-r requirements_file] [virtualenv options] ENVNAME
  • The -a option can be used to associate an existing project directory with the new environment.
    将一个虚拟环境与一个项目绑定,每次workon 虚拟环境,直接进入工程目录,在工程目录下工作
  • The -i option can be used to install one or more packages (by repeating the option) after the environment is created.
  • The -r option can be used to specify a text file listing packages to be installed. The argument value is passed to pip -r to be installed.
2、使用虚拟环境

如果我们平时想要进入某个虚拟环境,可以用下面的命令。
workon 虚拟环境名称
例如:workon spider
workon功能:

  • workon [虚拟环境名称]
    在不同虚拟环境中自由切换
  • workon
    列出在$HOME/.virtualenvs下安装的虚拟环境列表
3、离开虚拟环境
$ deactivate
4、删除虚拟环境
$ rmvirtualenv venvname
5、列出使用mkvirtualenv安装的虚拟环境列表

workon 或者lsvirtualenv

6、控制环境是否使用global site-packages, 切换是否使用global site-packages

toggleglobalsitepackages

7、复制虚拟环境
$ cpvirtualenv [source] [dest]
$ cpvirtualenv env1 env3
8、在WORKON_HOME目录下安装的全部虚拟环境上运行命令

allvirtualenv command with arguments

$ allvirtualenv pip install -U pip
9、mktmpenv

创建一个临时环境,当deactivate时,环境被删除

10、showvirtualenv [env]

Show the details for a single virtualenv.

11、cdvirtualenv [subdir]

此时在一个虚拟环境中,改变当前工作目录到该虚拟环境所在目录,如果后面加subdir,则直接进入虚拟环境下的子目录。

12、cdsitepackages [subdir]

改变当前工作目录到 site-packages for $VIRTUAL_ENV.

13、lssitepackages

列出当前激活的虚拟环境site-packages中的文件及子目录

14、mkproject
mkproject [-f|--force] [-t template] [virtualenv_options] ENVNAME

创建一个新的虚拟环境在WORKON_HOME 和工程目录在PROJECT_HOME,工程目录与虚拟环境绑定,每次workon ENVNAME 直接使用python虚拟环境在项目目录下工作

-f, --force Create the virtualenv even if the project directory already exists
15、setvirtualenvproject [virtualenv_path project_path]

将一个已经存在的虚拟环境和已经存在的工程绑定,每次workon ENVNAME 直接使用python虚拟环境在绑定项目目录下工作

16、cdproject

改变当前工作目录到一个具体的工程目录 for the active virtualenv.

17、wipeenv

删除当前虚拟环境中安装的所有的第三方包

18、virtualenvwrapper

打印virtualenvwrapper支持的命令和命令描述作为简单的帮助文档

注:

  • 版本支持Python 2.6-3.4.
  • virtualenvwrapper should be installed into the same global site-packages area where virtualenv is installed.

三、配置

可以通过配置环境变量来配置vitrualenvwrapper
1、PROJECT_HOME
PROJECT_HOME指定了使用mkproject创建的项目目录的位置。
The variable must be set and the directory created before mkproject is used.

2、VIRTUALENVWRAPPER_HOOK_DIR
指定用户自定义的hooks存放目录,默认是 $WORKON_HOME

3、VIRTUALENVWRAPPER_LOG_FILE
tells virtualenvwrapper where the logs for the hook loader should be written.默认不输出来自hooks的日志

4、VIRTUALENVWRAPPER_WORKON_CD
VIRTUALENVWRAPPER_WORKON_C默认值为1,在使用mkproject projectname创建虚拟环境及工程目录后,workon projectname默认会自动切换项目目录;若将环境变量VIRTUALENVWRAPPER_WORKON_CD设置为0,则workon projectname不会进入到工程目录,只激活虚拟环境。

注:
1、使用python和virtualenv:
During startup, virtualenvwrapper.sh finds the first python and virtualenv programs on the $PATH and remembers them to use later.
因此,确保在shell脚本配置文件中export PATH=/usr/local/bin:$PATH
在source /usr/local/bin/virtualenvwrapper.sh之前

如果不想在path中寻找,set the variable VIRTUALENVWRAPPER_PYTHON to the full path of the interpreter to use and VIRTUALENVWRAPPER_VIRTUALENV to the full path of the virtualenv binary to use. Both variables must be set before sourcing virtualenvwrapper.sh

2、产生的临时文件位置:
virtualenvwrapper creates temporary files in $TMPDIR. If the variable is not set, it uses /tmp. To change the location of temporary files just for virtualenvwrapper, set VIRTUALENVWRAPPER_TMPDIR.

推荐阅读更多精彩内容

  • 确保你已经有了 python 和 pip在您进一步之前,请确保您有 Python,并且可从您的命令行中获得。 你可...
    yiangdea阅读 969评论 0 1
  • 原文链接:www.cnblogs.com/cheungjustin/archive/2011/12/08/2281...
    sn_msw阅读 790评论 0 0
  • 一、使用虚拟环境的原因 在使用python开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题;亦或...
    kaley_ma阅读 11,509评论 5 15
  • 一、virtualenv 虚拟环境安装    virtualenv 工具可以用来在 Linux 操作系统中创建一个...
    常大鹏阅读 15,679评论 0 15
  • 块级元素和行内元素的区别 1.块级元素独占一行,默认情况下,其宽度自动填满其父元素宽度;而行内元素不会独占一行,相...
    azure_1122阅读 265评论 0 0