virtualenv:指定在系统范围和本地使用哪些包 [英] virtualenv: Specifing which packages to use system-wide vs local

查看:21
本文介绍了virtualenv:指定在系统范围和本地使用哪些包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
让 virtualenv 从你的全局站点包继承特定包

有没有办法为 Python 创建一个 virtualenv指定哪些包应该从系统范围的安装中使用(继承),哪些应该忽略从系统范围的安装?

Is there a way to create a virtualenv for Python and specify which packages should be used (inherited) from the system-wide installation, and which ones it should ignored from the system-wide installation?

更具体地说,例如,有一个系统范围的安装:

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:

  • 使用 numpyscipy 的系统范围安装
  • 忽略系统范围的matplotlib,让我安装/升级我自己的版本(使用pip -Umatplotlib).
  • Uses the system-wide installation of numpy and scipy
  • Ignores the system-wide matplotlib, and lets me install/upgrade my own versions of it (with pip -U matplotlib).

这可能吗?

推荐答案

最简单的方法是创建一个包含系统站点包的 virtualenv,然后安装您需要的版本:

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 --system-site-packages foo
$ source foo/bin/activate
$ pip install Django==1.4.3

您也可以在之后通过检查 pip freeze 的输出并删除您不想要的包来清理 virtualenv.(删除 system-site-packagespip uninstall 不再适用于较新版本的 virtualenv)

You can also clean up the virtualenv afterwards by checking the output of pip freeze and removing the packages that you do not want. (removing system-site-packages with pip uninstall does no longer work for newer versions of virtualenv)

另一种方法是创建一个干净的 virtualenv 并链接您需要的部分:

Another way would be to create a clean virtualenv and link the parts that you need:

$ virtualenv --no-site-packages foo
$ source foo/bin/activate
$ ln -s /usr/lib/python2.7/dist-packages/PIL* $VIRTUAL_ENV/lib/python*/site-packages

这些命令在非 unixish 环境中可能略有不同.路径还取决于您使用的系统.为了找出库的路径,启动python shell(没有激活的virtualenv),导入模块并检查module_name.__path__.例如

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.

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']

此外,如果您使用 --system-site-packages 创建了 virtualenv,则可以使用 pip install --upgrade -- 安装比系统中的版本更新的版本忽略安装的 numpy.

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.

这篇关于virtualenv:指定在系统范围和本地使用哪些包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆