Python virtualenv:指定在系统范围和本地使用哪些包
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14571454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
virtualenv: Specifing which packages to use system-wide vs local
提问by Amelio Vazquez-Reina
Possible Duplicate:
Make virtualenv inherit specific packages from your global site-packages
Is there a way to create a virtualenvfor Python and specify which packagesshould be used (inherited) from the system-wide installation, and which ones it should ignored from the system-wide installation?
有没有办法virtualenv为 Python创建一个并指定应该从系统范围的安装中使用(继承)哪些包,以及应该从系统范围的安装中忽略哪些包?
More specifically, say for example that there is a system-wide installation of:
更具体地说,例如,有一个系统范围的安装:
numpy
scipy
matplotlib
I would like to create a virtual environment such that:
我想创建一个虚拟环境,以便:
- Uses the system-wide installation of
numpyandscipy - Ignoresthe system-wide
matplotlib, and lets me install/upgrade my own versionsof it (withpip -U matplotlib).
- 使用系统范围的安装
numpy和scipy - 忽略系统范围的
matplotlib,让我安装/升级我自己的版本(使用pip -U matplotlib)。
Is this possible?
这可能吗?
采纳答案by bikeshedder
The simplest way to do this is to create a virtualenv which includes the system site packages and then install the versions that you need:
最简单的方法是创建一个包含系统站点包的 virtualenv,然后安装您需要的版本:
$ virtualenv --system-site-packages foo
$ source foo/bin/activate
$ pip install Django==1.4.3
You can also clean up the virtualenv afterwards by checking the output of (removing system-site-packages with pip freezeand removing the packages that you do not want.pip uninstalldoes no longer work for newer versions of virtualenv)
您还可以在之后通过检查输出(删除 system-site-packagespip freeze并删除您不想要的包来清理 virtualenv 。pip uninstall不再适用于较新版本的 virtualenv)
Another way would be to create a clean virtualenv and link the parts that you need:
另一种方法是创建一个干净的 virtualenv 并链接您需要的部分:
$ virtualenv --no-site-packages foo
$ source foo/bin/activate
$ ln -s /usr/lib/python2.7/dist-packages/PIL* $VIRTUAL_ENV/lib/python*/site-packages
The commands might be slightly different on a non-unixish environment. The paths also depend on the system you are using. In order to find out the path to the library start up the python shell (without an activated virtualenv), import the module and check module_name.__path__. e.g.
这些命令在非 unixish 环境中可能略有不同。路径还取决于您使用的系统。为了找出库的路径,启动 python shell(没有激活的 virtualenv),导入模块并检查module_name.__path__. 例如
Python 2.7.3 (default, Sep 26 2012, 21:51:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> PIL.__path__
['/usr/lib/python2.7/dist-packages/PIL']
Also if you have created your virtualenv with --system-site-packages, it is possible to install newer version than what is in the system with pip install --upgrade --ignore-installed numpy.
此外,如果您--system-site-packages使用pip install --upgrade --ignore-installed numpy.

