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:
For "myapp":
What do you think of this solution? Is it reliable for a distribuable release?
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With