[英]Can a virtualenv inherit from another?
I want to create one virtualenv
using another as the starting point, is this possible? 我想创建一个
virtualenv
使用另一个作为起点,这可能吗?
I have to use cases in mind: 我必须记住案例:
Let's say I have a two virtualenv
one for production and one for development. 假设我有两个
virtualenv
用于生产,一个用于开发。 The development environment requires the same packages as the production environment, but it requires others I don't want in the production environment. 开发环境需要与生产环境相同的包,但它需要在生产环境中不需要的其他包。 I don't want to install the common packages twice.
我不想两次安装普通包。
I want to experiment with a development version of a package, say matplotlib
for example. 我想尝试一个软件包的开发版本,例如
matplotlib
。 The development version of the package has the same requirements as the stable version. 该软件包的开发版本与稳定版本具有相同的要求。 So I create a
virtualenv
called matplotib_stable
and install the requirements and the stable version. 所以我创建了一个名为
matplotib_stable
的virtualenv
并安装了需求和稳定版本。 Then I create a second virtualenv
called matplotlib_dev
and use matplotlib_stable
as a starting point (for the matplotlib
requirements) but then I install the development version. 然后我创建了第二个名为
matplotlib_dev
virtualenv
并使用matplotlib_stable
作为起点(对于matplotlib
要求),然后我安装了开发版本。
How do I install from a local cache with pip? 如何使用pip从本地缓存安装? seems to address the issue of downloading packages, but I don't think it deals with modifying
sys.path
. 似乎解决了下载软件包的问题,但我认为它不涉及修改
sys.path
。
One solution is to use virtualenvwrapper
's add2virtualenv
command. 一种解决方案是使用
virtualenvwrapper
的add2virtualenv
命令。 This 这个
Adds the specified directories to the Python path for the currently-active virtualenv.
将指定的目录添加到当前活动的virtualenv的Python路径中。
So if I have two virtualenv
, ENV1
and ENV2
, and I want ENV2
to access the packages in ENV1
, then I need to: 所以如果我有两个
virtualenv
, ENV1
和ENV2
,我想让ENV2
访问ENV1
的包,那么我需要:
activate ENV2
: 激活
ENV2
:
workon ENV2
add ENV1
's site-packages directory using add2virtualenv
: 使用
add2virtualenv
添加ENV1
的site-packages目录:
add2virtualenv $WORKON_HOME/ENV1/lib/python2.6/site-packages
The above assumes $WORKON_HOME
is the location of your virtualenv directories, and that you are using python2.6, so obviously adjust those accordingly. 上面假设
$WORKON_HOME
是你的virtualenv目录的位置,并且你正在使用python2.6,所以显然相应地调整它们。
While this provides access to the packages it does not adjust the shell path. 虽然这提供了对包的访问,但它不会调整shell路径。 In other words, scripts installed to the
bin
directory are not accessible using this method. 换句话说,使用此方法无法访问安装到
bin
目录的脚本。
The following seems to work for me. 以下似乎对我有用。 Assume an old virtual environment you want to inherit from called
old
. 假设你要继承所谓的老虚拟环境中
old
。 Since you can specify which python version to use when creating a new environment, simply do: 由于您可以指定在创建新环境时使用哪个python版本,只需执行以下操作:
virtualenv -p path_to_venvs/old/bin/python --system-site-packages new_env
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.