virtualenv:指定要使用系统范围的软件包还是本地的软件包 [英] virtualenv: Specifing which packages to use system-wide vs local
问题描述
可能重复:
使virtualenv从全局站点包中继承特定的包
Possible Duplicate:
Make virtualenv inherit specific packages from your global site-packages
有没有一种方法可以为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:
- 使用
numpy
和scipy
在系统范围内进行安装
- 忽略系统级的
matplotlib
,并允许我安装/升级自己的版本(使用pip -U matplotlib
).
- Uses the system-wide installation of
numpy
andscipy
- Ignores the system-wide
matplotlib
, and lets me install/upgrade my own versions of it (withpip -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.pip uninstall
删除system-site-packages不再适用于较新版本的virtualenv)
You can also clean up the virtualenv afterwards by checking the output of (removing system-site-packages with pip freeze
and removing the packages that you do not want.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 --ignore-installed 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屋!