安装virtualenv 参考
在安装virtualenv之前,我们需要安装至少有一个版本的Python;因为virtualenv是python的一个第三方模块,必须基于python环境才能安装。
pip安装
» pip install virtualenv
Collecting virtualenv
Downloading virtualenv-15.0.3-py2.py3-none-any.whl (3.5MB)
100% |████████████████████████████████| 3.5MB 163kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.0.3
或者
pip3 install virtualenv
源码安装
下载源码
$ curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-X.X.tar.gz
$ tar xvfz virtualenv-X.X.tar.gz
$ cd virtualenv-X.X
$ [sudo] python setup.py install
virtualenv帮助
sudo find / -name virtualenv
// ...
ln -s /Library/Frameworks/Python.framework/Versions/3.6/bin/virtualenv /usr/local/bin/virtualenv
» virtualenv -h
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/local/opt/python/bin/python2.7)
--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.
使用virtualenv
创建独立运行环境
创建一个独立的Python运行环境,命名为 py2
» virtualenv --no-site-packages --python=python2.7 py2
Running virtualenv with interpreter /usr/local/bin/python2.7
New python executable in /Users/leeyi/workspace/py/py2/bin/python2.7
Also creating executable in /Users/leeyi/workspace/py/py2/bin/python
Installing setuptools, pip, wheel...done.
- 参数--no-site-packages,使得已经安装到系统Python环境中的所有第三方包都不会复制过来;当前版本这个选项是默认的;我们就得到了一个不带任何第三方包的“干净”的Python运行环境;
- --python=python2.7 指定python2.7环境;系统中安装了多个python版本时非常有用;默认情况下virtualenv会选取它的宿主python环境;
激活环境
workspace/py » source py2/bin/activate
(py2) workspace/py »
- 注意到命令提示符变了,有个(py2)前缀,表示当前环境是一个名为py2的Python环境。
安装第三方包
在py2环境下,用pip安装的包都被安装到py2这个环境下,系统Python环境不受任何影响。
pip install [package name]
退出环境
(py2) workspace/py » deactivate
workspace/py »
现在,回到了正常的环境,pip或python都是在系统Python环境下的命令。
virtualenv原理
- virtualenv是如何创建“独立”的Python运行环境的呢?
- 原理很简单,就是把系统Python复制一份到virtualenv的环境,用命令source venv/bin/activate进入一个virtualenv环境时,virtualenv会修改相关环境变量,让命令python和pip均指向当前的virtualenv环境。