python虚拟环境配置
原文地址
VirtualEnv用于在一台机器上创建多个独立的python运行环境,VirtualEnvWrapper为前者提供了一些便利的命令行上的封装。
1. pip安装virtualenv
pip install virtualenv #py2安装
pip3 install virtualenv #py3安装,这样用virtualenv创建的virtualenv默认python版本是py3
1.1 virtualenv创建虚拟环境并进入使用
说明文档
You must provide a DEST_DIR
Usage: virtualenv [OPTIONS] DEST_DIR
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose Increase verbosity.
-q, --quiet Decrease verbosity.
-p PYTHON_EXE, --python=PYTHON_EXE
The Python interpreter to use, e.g.,
--python=python2.5 will use the python2.5 interpreter
to create the new environment. The default is the
interpreter that virtualenv was installed with
(/usr/bin/python)
--clear Clear out the non-root install and start from scratch.
--no-site-packages DEPRECATED. Retained only for backward compatibility.
Not having access to global site-packages is now the
default behavior.
--system-site-packages
Give the virtual environment access to the global
site-packages.
--always-copy Always copy files rather than symlinking.
--unzip-setuptools Unzip Setuptools when installing it.
--relocatable Make an EXISTING virtualenv environment relocatable.
This fixes up scripts and makes all .pth files
relative.
--no-setuptools Do not install setuptools in the new virtualenv.
--no-pip Do not install pip in the new virtualenv.
--no-wheel Do not install wheel in the new virtualenv.
--extra-search-dir=DIR
Directory to look for setuptools/pip distributions in.
This option can be used multiple times.
--download Download preinstalled packages from PyPI.
--no-download, --never-download
Do not download preinstalled packages from PyPI.
--prompt=PROMPT Provides an alternative prompt prefix for this
environment.
--setuptools DEPRECATED. Retained only for backward compatibility.
This option has no effect.
--distribute DEPRECATED. Retained only for backward compatibility.
This option has no effect.
- 安装需要版本的python
指定virtualenv中的python版本
virtualenv --no-site-packages --python=2.7 env
Note:
创建virtualenv虚拟环境之前,系统中必须要安装有对应版本的python,并且卸载之后当前虚拟环境就无效了。系统中可以同时存在python2和python3,通过环境变量中的系统变量path(不是用户变量)控制cmd或者系统中使用哪个版本的python,哪个版本的路径在前面就优先使用哪个版本。
–no-site-packages表示不包括系统全局的Python安装包,这样会更令环境更干净
–python=python2.7指定Python的版本未系统已经安装了的Python2.7
env是建立的虚拟环境名称
没有安装python2.7或者使用命令virtualenv –no-site-packages –python=python2.7 env会出现The executable python does notexist 错误
1.2进入虚拟环境并激活
$ . env/bin/activate
1.3退出虚拟环境
$ deactivate
1.4删除虚拟环境
$ rm -r venv
2.virtualenvwrapper
原始virtualenv 创建使用方式太麻烦 virtualenvwrapper是一个工具使创建使用更方便
2.1 pip安装virtualenvwrapper
$ pip install virtualenvwrapper
2.2 初始化
第一次安装完成后需要,先设置一个变量WORKON_HOME,它将作为所有环境的前缀
$ mkdir -p $WORKON_HOME
$ export WORKON_HOME=~/Envs
$ source /usr/local/bin/virtualenvwrapper.sh
可以将初始化文件写入用户环境变量
$ vi .bashrc
# 加入
source /usr/local/bin/virtualenvwrapper.sh
export WORKON_HOME="虚拟环境绝对路径"
- 创建:mkvirtualenv [虚拟环境名称]
- 删除:rmvirtualenv [虚拟环境名称]
- 进入:workon [虚拟环境名称]
- 退出:deactivate
所有的虚拟环境,都位于/home/.virtualenvs目录下
创建指定版的虚拟环境:
(test-3.5) yangqian@ubuntu:~$ mkvirtualenv -p python3.5 test-3.5