2

I have set up a python virtual env using

$ virtualenv --system-site-packages env

It works fine on the machine where I have set up the virtual env and I can access all the global packages in my script.

I then copied this virtual env to a different machine by following these steps-

  1. Making the virtual env relocatable on my local machine $ virtualenv --relocatable env
  2. Copying the env folder over to the remote machine.
  3. Modify the env/bin/activate script to change the VIRTUAL_ENV path on the remote machine

With this I can access all my local packages just fine. But it cannot access the system level packages for some reason. The system level package I am trying to use is psycopg2.

However doing something like below on the remote machine gives me an access to the psycopg2 module

$ ln -s /usr/lib64/python2.7/dist-packages/psycopg2 $virtual_env/lib/python2.7/site-packages

or

$ cp -R /usr/lib64/python2.7/dist-packages/psycopg2 $virtual_env/lib/python2.7/site-packages

Is there any way I can get this working without having to do this hack ? How does the --system-site-packages option work? From what I see --system-site-packages option does not do a hard copy of system level packages to the virtual env. So does it set some kind of a path variable in the scripts which I can change and get this working ?

XAMPPRocky
  • 3,160
  • 5
  • 25
  • 45

1 Answers1

4

You don't copy virtualenvs, you recreate them. They are not portable across machines at all and not portable across folders by default (unless you specify --relocatable).

pip freeze gives you a list of installed packages, save that list into a file (usually called requirements.txt), copy it to the new machine, create the virtualenv, activate it and run pip install -r requirements.txt


Additionally, using --system-site-packages is rarely a good idea. Unless you cannot have a compiler on the machine, simply install the postgres development headers (postgresql-dev or a similar package) and then pip install psycopg2 inside your virtualenv.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • As mentioned in my question I am already using --relocatable.That makes most of the path variables relative so its portable.For some reason my firm is not in favor of recreating virtual env on various servers.The idea is to have the complete env ready, bundle this up and deploy it on various servers. The script has everything it needs to execute on this env. But the issue comes into picture when I am trying to access a system level package. My question is how does it work internally? Is there any path variable I can change and get this working like the way we do it for activate script ? – Gunjan Gangwani Mar 23 '16 at 15:48
  • correction above - "The script has everything it needs to execute on this env." - "The env has everything needed to execute the script". In addition I am already doing everything you mentioned above to set up the virtual env on my local machine like `pip install -r requirements.txt`. Sorry but this did not answer my question. – Gunjan Gangwani Mar 23 '16 at 16:01