您的位置:首页 > 编程语言 > Python开发

ubuntu下Python虚拟环境配置

2018-01-15 09:53 363 查看

Python虚拟环境配置

VirtualEnv用于在一台机器上创建多个独立的python运行环境,VirtualEnvWrapper为前者提供了一些便利的命令行上的封装。

1.安装virtualenv

pip install virtualenv   #py2安装


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
<
a465
/li>

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

2.1 pip安装virtualenvwrapper

$ pip install virtualenvwrapper


2.2 初始化

将初始化文件写入用户环境变量

$ vi .bashrc

# 加入下面两行配置
source /usr/local/bin/virtualenvwrapper.sh
# 虚拟环境指定路径,不指定默认使用~/.virtualenvs
export WORKON_HOME="虚拟环境绝对路径"


创建:mkvirtualenv [虚拟环境名称]

删除:rmvirtualenv [虚拟环境名称]

进入:workon [虚拟环境名称]

退出:deactivate

所有的虚拟环境,都位于/home/.virtualenvs目录下

创建指定版的虚拟环境:

yangqian@ubuntu:~$ mkvirtualenv -p python3.5 test-3.5
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: