Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a virtualenv from another virtualenv

Can we create a virtualenv from an existing virtualenv in order to inherit the installed libraries?

In detail:

I first create a "reference" virtualenv, and add libraries (with versions fixed):

virtualenv ref
source ref/bin/activate
pip install -U pip==8.1.1     # <- I want to fix the version number
pip install -U wheel==0.29.0  # <- I want to fix the version number

Then:

virtualenv -p ref/bin/python myapp
source myapp/bin/activate
pip list

I get:

pip (1.4.1)
setuptools (0.9.8)
wsgiref (0.1.2)

How to get my installed libraries?

Similar question

I saw a similar question: Can a virtualenv inherit from another?.

But I want a isolated virtualenv which didn't use the referenced virtualenv, except for libraries installation. So, adding the specified directories to the Python path for the currently-active virtualenv, is not the solution.

Why doing that?

Well, we have an integration server which builds the applications (for releases and continuous integration) and we want to keep the control on libraries versions and make the build faster.

Create a relocatable virtualenv

I think I could use a relocatable virtualenv, that way:

  1. create the ref virtualenv
  2. make it relocatable: ``virtualenv --relocatable ref```

For "myapp":

  • copy ref to myapp

What do you think of this solution? Is it reliable for a distribuable release?

like image 914
Laurent LAPORTE Avatar asked Sep 09 '16 10:09

Laurent LAPORTE


People also ask

Can we copy virtualenv?

virtualenv-clone will be installed inside newenv. Now while logged-in as newenv we can create a copy of any existing environment. For example creating the copy of ProjectAenv: (newenv): virtualenv-clone ProjectAenv ProjectBenv (newenv): deactivate # to come out from newenv.

What is virtualenv clone?

Project description virtualenv cloning script. A script for cloning a non-relocatable virtualenv. Virtualenv provides a way to make virtualenv's relocatable which could then be copied as we wanted.


1 Answers

You can solve your problem by using .pth files. Basically you do this:

virtualenv -p ref/bin/python myapp
realpath ref/lib/python3.6/site-packages > myapp/lib/python3.6/site-packages/base_venv.pth

After doing this and activating myapp, if you run pip list you should see all the packages from ref as well. Note that any packages installed in myapp would hide the respective package from ref.

like image 159
dcmm88 Avatar answered Sep 23 '22 07:09

dcmm88