18

Sometime there is a need to create a copy of exiting virtualenv to test extra features of our project.

In such a case we need to create a copy of the existing environment and add few more requirements.

One of the way to create a copy of existing virtualenv is mentioned below:

# while using existing virtualenv
(oldenv): pip freeze > requirements.txt

# after creating and logged-in to new virtualenv
(newenv): pip install -r requirements.txt

# where requirements.txt is the same file created using (oldenv)

But I personally do not prefer this way. Why?? Because of installing the same packages which are already there which we can re-use. Re-using or say copying will not only reduce the extra time and effort but will also reduce the Internet bandwidth usage to none, specially for limited and slow Internet users.

Cool! Then what are the other ways to create a copy of existing virtualenv locally?

I know two other ways which I'm mentioning below. Hope these will be helpful.

2 Answers 2

24

First way to create a copy of existing virtualenv, say ProjectAenv:

  1. Copy the ProjectAenv to some other name or directory:

    cp -R /home/sk/Path1/ProjectAenv /home/sk/Path2/ProjectBenv
    

    where /home/sk/Path1/ProjectAenv is the absolute path of the virtualenv ProjectAenv

  2. Then open the activate file of newly copied ProjectBenv using your preferred editor:

    gedit /home/sk/Path2/ProjectBenv/bin/activate
    
  3. Find the line VIRTUAL_ENV="/home/sk/Path1/ProjectAenv" and replace it with:

    VIRTUAL_ENV="/home/sk/Path2/ProjectBenv"
    

    Save and close the file.

Thats it!! Your new environment ProjectBenv is copied from ProjectAenv and ready to use. I'm using this way for a long time and never got any issues yet, although I recommend the 2nd way of copying.

Second way to create a copy of existing virtualenv(Recommended):

  1. Download virtualenv-clone:

    wget https://pypi.python.org/packages/source/v/virtualenv-clone/virtualenv-clone-0.2.6.tar.gz
    tar -zxvf virtualenv-clone-0.2.6.tar.gz
    cd virtualenv-clone-0.2.6
    
  2. Install virtualenv-clone inside any virtualenv environment:

    virtualenv newenv            # create a new environment. You can use any existing one.
    source newenv/bin/activate   # login to newenv
    (newenv): python setup.py install  # install virtualenv-clone in newenv
    # be sure that you are inside the directory "virtualenv-clone-0.2.6"
    

    virtualenv-clone will be installed inside newenv.

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

Please comment for any suggestions or changes.

2
  • 7
    The first approach is bad. In that approach, you just changed the location of virtual environnent. But the pip command has a shebang which uses the python location while building. Therefore, you wont be able to run pip if you go about copying like Method 1. Other shell scripts that are in you bin folder will not work as well for the same reason.
    – user665159
    Mar 14, 2017 at 8:58
  • 1
    For completeness I would like to add a comment to the answer of @SauravKumar: Also the second method utilising the package virtualenv-clone results in an incorrect path to the old python executable in the shebang line.
    – djoerch
    Nov 13, 2018 at 21:25
3

Just want to add to the manual method of @SauravKumar. Fixing the path in the activate script is necessary, but not sufficient. You also need to fix the path in other scripts like pip, pip2, etc.

The easiest thing to do is search for your path inside the virtual environment bin folder like:

root@www:/var/www/app/venv/bin# grep "/tmp/app/HadithHouseWebsite/venv" * -R
activate:VIRTUAL_ENV="/tmp/app/HadithHouseWebsite/venv"
activate.csh:setenv VIRTUAL_ENV "/tmp/app/HadithHouseWebsite/venv"
activate.fish:set -gx VIRTUAL_ENV "/tmp/app/HadithHouseWebsite/venv"
django-admin:#!/tmp/app/HadithHouseWebsite/venv/bin/python2.7
django-admin.py:#!/tmp/app/HadithHouseWebsite/venv/bin/python2.7
easy_install:#!/tmp/app/HadithHouseWebsite/venv/bin/python2.7
easy_install-2.7:#!/tmp/app/HadithHouseWebsite/venv/bin/python2.7
pbr:#!/tmp/app/HadithHouseWebsite/venv/bin/python2.7
pip:#!/tmp/app/HadithHouseWebsite/venv/bin/python2.7
pip2:#!/tmp/app/HadithHouseWebsite/venv/bin/python2.7
pip2.7:#!/tmp/app/HadithHouseWebsite/venv/bin/python2.7
python-config:#!/tmp/app/HadithHouseWebsite/venv/bin/python
sqlformat:#!/tmp/app/HadithHouseWebsite/venv/bin/python2.7
wheel:#!/tmp/app/HadithHouseWebsite/venv/bin/python2.7

Then go through these files and change the ones you need. Mostly you need to change the activate* files and pip* files.

Hope this helps.

1
  • 1
    Running the following from within the bin directory will make the changes. sed -i "s,{OLD_PATH},{NEW_PATH}," *
    – rcary2
    Nov 30, 2020 at 19:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .