Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

virtualenv - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...