Reputation: 4313
I have an existing virtualenv with a lot of packages but an old version of Django.
What I want to do is duplicate this environment so I have another environment with the exact same packages but a newer version of Django.
How can I do this?
Upvotes: 185
Views: 190418
Reputation: 1
pip works, but it's a problem on a computer without internet.
I wrote a small code for this, it worked for me. I'm writing it here because maybe it will be useful for someone else.
( Note: I tested it on Windows )
import os
# The new address of our script folder
script_folder = r'D:\Python proqrams\pdf_to_excel\venv\Scripts'
# the old address of our venv folder
old_path = rb'C:\Users\AVG-dell\Desktop\pdf_to_excel\venv'
# the new address of our venv folder
new_path = rb"D:\Python proqrams\pdf_to_excel\venv"
def find_replace( folder ):
names = os.listdir( folder )
for name in names:
current_path = os.path.join( folder, name )
if os.path.isdir( current_path ):
find_replace( current_path )
elif os.path.isfile( current_path ) :
try:
with open( current_path ,'rb' ) as f:
data = f.read()
if old_path in data:
print( current_path )
data2 = data.replace( old_path , new_path )
with open( current_path , 'wb' ) as f:
f.write(data2)
except:
pass
find_replace( script_folder )
print('completed')
Upvotes: 0
Reputation: 1166
Here is my go to command for cloning python virtual environments.
packs=`source-path/bin/pip freeze` && python3 -m venv <env-name> && target-path/bin/pip install $packs
Conventions used in above command:
/home/john/envs/oldenv
.myenv
, it can be a path as well e.g.
/home/john/envs/myenv
/home/john/envs/<env-name>
Advantages of using this or why i prefer this
In some cases you might want to exclude global packages from while cloning env you can replace source-path/bin/pip freeze
with source-path/bin/pip freeze --local
, more about --local
here
Upvotes: 2
Reputation:
In case you use pip "venv". I copy pasted the folder holding the virtual environment and manually changed the files in the bin folder of the copied folder. I don't know if its efficient,but it works!
Upvotes: 1
Reputation: 3426
Easiest option is using virtualenv-clone
package.
To duplicate venv1
to venv2
, follow these steps:
Install virtualenv-clone
in either venv1
or a dummy virtual environment venv_dummy
. To create venv_dummy
:
python -m virtualenv venv_dummy
source venv_dummy/bin/activate
To install virtualenv-clone
:
(venv_dummy): pip install virtualenv-clone
To duplicate venv1
to venv2
:
(venv_dummy): virtualenv-clone venv1/ venv2/
Upvotes: 22
Reputation: 1645
If you are using Anaconda you can just run:
conda create --name myclone --clone myenv
This will copy myenv
to the newly created environment called myclone
.
Upvotes: 13
Reputation: 1192
virtualenvwrapper
provides a command to duplicate virtualenv
cpvirtualenv ENVNAME [TARGETENVNAME]
Upvotes: 18
Reputation: 33844
The easiest way is to use pip to generate a requirements file. A requirements file is basically a file that contains a list of all the python packages you want to install (or have already installed in case of file generated by pip), and what versions they're at.
To generate a requirements file, go into your original virtualenv, and run:
pip freeze > requirements.txt
This will generate the requirements.txt file for you. If you open that file up in your favorite text editor, you'll see something like:
Django==1.3
Fabric==1.0.1
etc...
Now, edit the line that says Django==x.x
to say Django==1.3
(or whatever version you want to install in your new virtualenv).
Lastly, activate your new virtualenv, and run:
pip install -r requirements.txt
And pip will automatically download and install all the python modules listed in your requirements.txt file, at whatever versions you specified!
Upvotes: 250
Reputation: 474003
Another option is to use virtualenv-clone
package:
A script for cloning a non-relocatable virtualenv.
Upvotes: 40
Reputation: 94237
Can you not simply:
Upvotes: 0