Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recreate a virtual env in python

I have installed virtualenv and virtualenvwrapper on Ubuntu 16.04 I have created one enviroment named env1

$ sudo apt-get install python-pip
$ pip install virtualenv
$ pip install --upgrade pip 
$ pip install virtualenvwrapper
$ export WORKON_HOME=~/Envs
$ mkdir -p $WORKON_HOME
$ source /usr/local/bin/virtualenvwrapper.sh
$ mkvirtualenv env1

Once in (env1) I have installed several packages

(env1) $ pip install numpy
(env1) $ pip install scipy
(env1) $ pip install matplotlib
(env1) $ apt-get install python-tk

I have also installed opencv3 (I am not copying how because is too long)

I am using env1 for a specific project.

Now I want to start another project using the same packages, but I also want to add other packages.

I have created env2, and I was wondering if it is possible to copy env1 to env2 without the need to re-install everything again from scratch.

like image 471
daniel_hck Avatar asked Apr 17 '26 21:04

daniel_hck


2 Answers

Your best option is to do this:

virtualenv-1:

pip freeze > requirements.txt 

virtualenv-2:

pip install -r requirements.txt

Assuming they are both the on the same system and use the same Python, it is somewhat possible to just copy the site-packages:

cp -Rp /environments/virtualenv-1/lib/python2.7/site-packages \
       /environments/virtualenv-2/lib/python2.7/site-packages

This won't necessarily work though:

  • some packages will install dependencies and other things into /bin/ or elsewhere. most don't, but many do.
  • if the python versions for the virtualenvs differ -- even on a minor version -- that can break libraries that use c extensions.

So your best bet is to pip freeze and reinstall from that file.

like image 150
Jonathan Vanasco Avatar answered Apr 19 '26 10:04

Jonathan Vanasco


pip install virtualenvwrapper and use the cpvirtualenv command

cpvirtualenv ENVNAME [TARGETENVNAME]

http://virtualenvwrapper.readthedocs.io/en/latest/command_ref.html#cpvirtualenv

Remember to heed the warning

Copying virtual environments is not well supported. Each virtualenv has 
path information hard-coded into it, and there may be cases where the copy 
code does not know to update a particular file. Use with caution.
like image 34
dinosaurwaltz Avatar answered Apr 19 '26 10:04

dinosaurwaltz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!