Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to copy modules from one virtualenv to another

Is it possibe to copy python modules from one virtualenv to another.If so how is this done?

like image 609
bosco- Avatar asked Jan 09 '12 19:01

bosco-


People also ask

Can virtualenv be copied?

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.

Can I move a virtualenv folder to another computer?

Yes, this should be possible if you haven't done anything that depends on the current directory of the virtualenv. However, if you have the choice, the best thing to do is to create new virtualenv and start using the new virtualenv instead.

Does virtualenv inherit packages?

If you build with virtualenv --system-site-packages ENV , your virtual environment will inherit packages from /usr/lib/python2. 7/site-packages (or wherever your global site-packages directory is).


2 Answers

As long as you're moving them from one virtualenv to another on the same machine, you could easily just do:

$ cp -r [env1]/lib/pythonX.X/site-packages/* [env2]/lib/pythonX.X/site-packages/ 

However, if the environments are on different machines or utilizing different versions of python or some other major difference, it's probably not a good idea. In general it's much safer to generate a requirements.txt, and then use that to load up all the same modules in the other environment. You can create the file manually if you like, but it's easier to just use pip.

$ pip freeze -E [env1] > requirements.txt 

Or, if your virtualenv is activated already, you can simply do:

$ pip freeze > requirements.txt 

Then, in your other environment, you can do:

$ pip install -E [env2] -r /path/to/requirements.txt 
like image 163
Chris Pratt Avatar answered Sep 18 '22 17:09

Chris Pratt


I am working on a 64bit machine with Ubuntu-14.04-64. I compiled and installed python-3.4.3 to /opt/python3.4/ and created a vitualenv based on this python.

mkvirtualenv -p /opt/python3.4/bin/python venv1 

Also for ease:

sudo apt-get install virtualenvwrapper 

With the venv installed and working with PyQt5 successfully (the hard bit) plus numpy, scipy, ipython etc. I installed virtualenv-clone:

workon myvenv pip install virtual-clone deactivate 

and then ran:

virtualenv-clone venv1 venv2 

PyQt5 works this way. The command-line prompt still names venv1 as active but within ~/.virtualenv/venv2

cat activate* | grep "venv1" 

shows 3 entries within the three files activate, activate.csh, and activate.fish

In activate, change

if [ "x(myvenv1) " != x ] ; then         PS1="(myvenv1) $PS1" else 

to

...         PS1="(myvenv2) $PS1" ... 

In activate.csh change

if ("venv1" != "") then         set env_name = "venv1" else 

to

...     set env_name = "venv2" ... 

In activate.fish change

if test -n "(venv1) "         printf "%s%s%s" "(venv1) " (set_color normal) (_old_fish_prompt)         return end 

to

...     printf "%s%s%s" "(venv2) " (set_color normal) (_old_fish_prompt) ... 

Now when you source ~/.virtualenv/venv2/bin/activate or workon venv2 the command prompt will correctly display your environment (the cloned copy of venv1).

Edit: this doesn't answer the question "How to copy modules from one virtualenv to another" but I'm pretty sure the result is in many cases the desired one, namely the creation of a new venv based on a previously created one which includes (all of) the previously installed modules.

like image 45
Chris Hanning Avatar answered Sep 19 '22 17:09

Chris Hanning