80

I'm new to virtualenv but I'm writting django app and finally I will have to deploy it somehow.

So lets assume I have my app working on my local virtualenv where I installed all the required libraries. What I want to do now, is to run some kind of script, that will take my virtualenv, check what's installed inside and produce a script that will install all these libraries on fresh virtualenv on other machine. How this can be done? Please help.

mnowotka
  • 16,430
  • 18
  • 88
  • 134

3 Answers3

151

You don't copy paste your virtualenv. You export the list of all the packages installed like -

pip freeze > requirements.txt

Then push the requirements.txt file to anywhere you want to deploy the code, and then just do what you did on dev machine -

$ virtualenv <env_name>
$ source <env_name>/bin/activate
(<env_name>)$ pip install -r path/to/requirements.txt

And there you have all your packages installed with the exact version.

You can also look into Fabric to automate this task, with a function like this -

def pip_install():
    with cd(env.path):
        with prefix('source venv/bin/activate'):
            run('pip install -r requirements.txt')
Bibhas Debnath
  • 14,559
  • 17
  • 68
  • 96
  • Fair enough, but that's a shame there is no dedicated commandand and fabric integration ;) – mnowotka Feb 04 '13 at 10:45
  • 1
    I just added fabric to the answer. You can easily write a fabric function to do the same. Hardly few lines of code. – Bibhas Debnath Feb 04 '13 at 10:47
  • Added a sample function to get you started. – Bibhas Debnath Feb 04 '13 at 10:49
  • 2
    This is totally insufficient if you've used the virtual environment for anything more complicated than tracking package versions. E.g., if you have different python paths for virtual virtual environments, or are configuring other aspects of the environment. Even something as simple as the python version is use is overlooked by this approach! – Shon Oct 20 '17 at 21:14
  • However, as I've looked in to this more, it seems that virtualenv is really not equipped for supporting containment of this kind of more complex environment management , even though they support such customization... so maybe I am just using the tool wrong. – Shon Oct 20 '17 at 21:25
  • What if the destination computer does not have internet connectivity? That's the case with a lot of production deployments. Is there any solution for that case? – Durga Swaroop Nov 19 '20 at 07:43
  • What does this have to do with `export`? – Wolfpack'08 Nov 12 '22 at 15:13
4

You can install virtualenvwrapper and try cpvirtualenv, but the developers advise caution here:

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 it needs to update a particular file. Use with caution.

Shon
  • 3,989
  • 1
  • 22
  • 35
-2

If it is going to be on the same path you can tar it and extract it on another machine. If all the same dependencies, libraries etc are available on the target machine it will work.

  • This is ambiguous. How does one ensure that all of the "dependencies, libraries etc are available on the target machine?" – Chris Wong May 06 '22 at 20:05