1. 程式人生 > >Python 開發環境管理 virtualenvwrapper

Python 開發環境管理 virtualenvwrapper

virtualenvwrapper 時一個基於virtualenv之上的工具,它將所有的虛擬環境統一管理。

安裝

使用pip進行安裝

# whoami at iamwho in [~]
$ pip install virtualenvwrapper -i https://pypi.douban.com/simple/

初次啟動

  • Ubuntu自帶python安裝的virtualenvwrapper
source /usr/local/bin/virtualenvwrapper.sh
  • miniconda或者anaconda安裝的virtualenvwrapper
source miniconda2/bin/virtualenvwrapper.sh

還可以將該命令新增到~/.bashrc~/.profie等shell啟動檔案中,以便登陸shell後可直接使用virtualenvwrapper提供的命令。

對於virtualenvwrapper的配置:

if [ `id -u` != '0' ]; then

  export VIRTUALENV_USE_DISTRIBUTE=1        	# <-- Always use pip/distribute
  export WORKON_HOME=$HOME/.virtualenvs       	# <-- Where all virtualenvs will be stored
source /usr/local/bin/virtualenvwrapper.sh export PIP_VIRTUALENV_BASE=$WORKON_HOME export PIP_RESPECT_VIRTUALENV=true fi

將上面的配置新增到 ~/.bashrc 的末尾或者~/.zshrc

虛擬環境

virtualenvwrapper預設將所有的虛擬環境放在~/.virtualenvs目錄下管理,

# whoami at iamwho in [~]
$ source miniconda2/bin/virtualenvwrapper.sh 
virtualenvwrapper.user_scripts creating /home/whoami/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/whoami/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /home/whoami/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/whoami/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/whoami/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/whoami/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/whoami/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/whoami/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/whoami/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/whoami/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/whoami/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/whoami/.virtualenvs/get_env_details

可以修改環境變數WORKON_HOME來指定虛擬環境的儲存目錄。

常用命令

  • mkvirtualenv

    usage:

    mkvirtualenv 	[-a project_path] 
    				[-i package] 
    				[-r requirements_file] 
    				[virtualenv options] env_name
    
  • workon

    workon env_name
        Deactivate any currently activated virtualenv
        and activate the named environment, triggering
        any hooks in the process.
    
    workon == lsvirtualenv
        Print a list of available environments.
        (See also lsvirtualenv -b)
    
    workon (-h|--help)
    	Show this help message.
    
    workon (-c|--cd) envname
        After activating the environment, cd to the associated
        project directory if it is set.
    
    workon (-n|--no-cd) envname
    	After activating the environment, do not cd to the
    	associated project directory.
    
  • lsvirtualenv is equal to workon without params

  • deactivate 跟virtualenv的一樣

  • rmvirtualenv

    $ rmvirtualenv --help
    Removing --help...
    Did not find environment /home/whoami/.virtualenvs/--help to remove.
    

以上為常用的命令足夠用,具體引數其實跟virtualenv差不多。