21

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.

Community
  • 1
  • 1
Yann
  • 33,811
  • 9
  • 79
  • 70

1 Answers1

17

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.

Yann
  • 33,811
  • 9
  • 79
  • 70
  • When I installed the same package with different versions on both virtualenvs, I found out the outside env (ENV1) was taking precedence over the internal env (ENV2). Looks like add2virtualenv puts its paths earlier on the sys.path import list. An easy solution is to follow the command above with this one: `add2virtualenv $WORKON_HOME/ENV2/lib/python2.6/site-packages` and then it works like a charm. – Yonatan Nov 09 '14 at 07:05
  • @Yann Thanks Yann!I install pylint in ENV1,then `add2virtualenv $WORKON_HOME/ENV1/lib/python3.4/site-packages`,but pylint not working! – HelloNewWorld Jun 21 '15 at 14:15
  • 1
    @HelloNewWorld I haven't used `virtualenv` and `vertualenvwrapper` in a while, why not post a new question about this issue? – Yann Jun 22 '15 at 11:06
  • @Yann agree. Might be time for an updated answer for Python 3 and `venv`. – Jacktose Mar 09 '18 at 17:38