简体   繁体   中英

Can a virtualenv inherit from another?

I want to create one virtualenv using another as the starting point, is this possible?

I have to use cases in mind:

  1. Let's say I have a two virtualenv one for production and one for development. 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.

  2. I want to experiment with a development version of a package, say matplotlib for example. 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. 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.

How do I install from a local cache with pip? seems to address the issue of downloading packages, but I don't think it deals with modifying sys.path .

One solution is to use virtualenvwrapper 's add2virtualenv command. This

Adds the specified directories to the Python path for the currently-active virtualenv.

So if I have two virtualenv , ENV1 and ENV2 , and I want ENV2 to access the packages in ENV1 , then I need to:

  1. activate ENV2 :

    workon ENV2

  2. add ENV1 's site-packages directory using add2virtualenv :

    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.

While this provides access to the packages it does not adjust the shell path. In other words, scripts installed to the bin directory are not accessible using this method.

The following seems to work for me. Assume an old virtual environment you want to inherit from called old . Since you can specify which python version to use when creating a new environment, simply do:

virtualenv -p path_to_venvs/old/bin/python --system-site-packages new_env

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM