2

What's the easiest way to clone a virtual environment created with venv?

Is there a single-command solution to do this?

This question is different from How to duplicate virtualenv, because I'm asking a single-command solution. Something like the option --clone used by Conda. Moreover this question is about venv not virtualenv

spookylukey
  • 6,380
  • 1
  • 31
  • 34
MarcoM
  • 1,093
  • 9
  • 25
  • HI Robby, this questino is specifically for venv (I'd like to avoid installing virtualenv and the related packages suggestend in some answers of the question of your link). Creating and using requirements.txt could be a possible solution, but I'm quite surprised there is not an intuitive single-command solution to this (in my opinion) common operation... – MarcoM Sep 09 '21 at 07:30
  • You might be interested to check [virtualenv-clone](https://github.com/edwardgeorge/virtualenv-clone). – Niko Föhr Sep 09 '21 at 08:36
  • By the way, *why* would you want to clone a virtual environment? – Niko Föhr Sep 09 '21 at 08:38
  • The idea is to test a script in an environment where a specific packaged is being updated. So I need to 1)Clone the reference environment 2)Update the specific package 3) Test the script – MarcoM Sep 09 '21 at 10:02
  • there appear to be several single command answers in the question you linked, it doesn't seem like this question is really asking anything different except that those answers did not work for you (which is unfortunate, but a good answer to this question would also be a good answer to that question) – Sasha Kondrashov Sep 30 '22 at 17:43

3 Answers3

2

May be you do not have to copy it, just make a symbolic link ln -s /path/to/venv .

By the way, I have been using python-poetry to manage virtual environments for a long time.

Waket Zheng
  • 5,065
  • 2
  • 17
  • 30
  • 1
    I'll give a look to python-poetry, seems interesting, thanks for the suggestion. As for the link it would not work if I need to change the clone – MarcoM Sep 09 '21 at 10:23
2

There is no single command. However it is good practice to create a requirements.txt file in your venvs anyway. To copy your venv to a new location.

Activate your original venv. Get your requirements by doing the following in your virtual environment.

pip freeze > requirements.txt

Deactivate your venv.

Then you would want to create a new venv in your desired location. If you are using a specific version of Python, make sure you install it here also.

Copy over the requirements.txt file into the working directory.

Activate your new venv. Then run:

pip install -r requirements.txt

This will install all of the modules (at correct version) from one venv into another.

sh37211
  • 1,411
  • 1
  • 17
  • 39
Jeremy Savage
  • 944
  • 5
  • 14
  • 1
    The use-case I have for wanting to clone a venv is so that I could have all of my base/core packages preinstalled at the proper versions. Then I could clone that and make adjustments to the cloned env w/o having to spend ~30mins re-installing all of those base packages again. – refriedjello Mar 01 '22 at 20:23
  • 1
    You can just copy the site-packages if you really have that many modules, although I've never had any issues with pip install taking too long even with repos with hundreds of modules. cp -r [path_to_env1]/lib/pythonX.X/site-packages/* [path_to_env2]/lib/pythonX.X/site-packages/ should do the job to copy all of the packages to a new venv (bear in mind you must set it up first). – Jeremy Savage Mar 03 '22 at 14:43
0

just copying the main folder of the venv environment will work just fine. You might have to only activate the environment again in the new venv

Duffy
  • 123
  • 7
  • 2
    Hi Duffy, I've tried this but this is not working. The new venv still has hardcoded path references to the original venv. – MarcoM Sep 09 '21 at 07:09
  • once you re activate the activate script it will detect the path at work. I have been doing this to make backups for my environments. – Duffy Sep 13 '21 at 03:13
  • Do you use "source " with Bash to activate? – MarcoM Sep 13 '21 at 15:45
  • 2
    I've tried to do that `cp` env and then `source env/bin/activate` and python still points to the original one. So it doesn't work as expected. – fo2rist Mar 21 '22 at 23:36