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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:51:33  来源:igfitidea点击:

virtualenv: Specifing which packages to use system-wide vs local

pythonvirtualenvpip

提问by Amelio Vazquez-Reina

Possible Duplicate:
Make virtualenv inherit specific packages from your global site-packages

可能的重复:
使 virtualenv 从您的全局站点包中继承特定的包

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 numpyand scipy
  • Ignoresthe system-wide matplotlib, and lets me install/upgrade my own versionsof it (with pip -U matplotlib).
  • 使用系统范围的安装numpyscipy
  • 忽略系统范围的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 pip freezeand removing the packages that you do not want.(removing system-site-packages with pip uninstalldoes no longer work for newer versions of virtualenv)

您还可以在之后通过检查输出pip freeze并删除您不想要的包来清理 virtualenv 。(删除 system-site-packagespip 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.