How to get virtualenv to use dist packages on Ubuntu?

I know that virtualenv, if you did not pass the --no-site-packages argument when creating a new virtual environment, will associate packages in /usr/local/lib/python2.7/site-packages (for Python 2.7) with the newly created virtual environment . On Ubuntu 12.04 LTS, I have three locations where Python 2.7 packages can be installed (using the default Ubuntu Python 2.7):

  • /usr/lib/python2.7/dist-packages : I have my global installation of ipython, scipy, numpy, matplotlib packages, which are harder and harder for me to install separately (and all their dependencies) if they are not available through the scipy stack .
  • /usr/local/lib/python2.7/site-packages : this is empty and I think it will remain the same on Ubuntu if I do not install the package from the source code.
  • /usr/local/lib/python2.7/dist-packages : it has very important local packages for astronomy, especially those related to PyRAF, STScI, etc., and they are extremely complex and time-consuming to install separately.

Please note that a global directory such as /usr/lib/python2.7/site-packages does not exist on my system. Also note that my global installation is ipython, scipy, etc. Allows me to use these packages on the fly without having to start / activate the virtual environment every time.

Naturally, now I want to use virtualenv to create one virtual environment in the user's home directory, which I will use for future projects. However, I would like this virtual environment to be created to link / copy all my packages in places (1) and (3) in the list above. The main reason for this is because I don’t want to go through the pip install process (if at all possible) to reinstall ipython, scipy, astro packages, etc. For this (and possibly another) virtual environment.

Here are my questions:

  • Is there any way to tell virtualenv that I would like it to bundle / copy packages in these two dist-packages directories for virtual environments that will be created in the future?
  • When I end up updating my global installation of scipy, ipython, etc. in the two dist-packages directories, will this also update / change the packages used by my virtual environment (and which they originally received when creating the virtual virtual disk)?
  • If I ever installed a package from a source in Ubuntu, will it go to /usr/local/lib/python2.7/dist-packages or /usr/local/lib/python2.7/site-packages ?

Thanks in advance for your help!

+13
python ubuntu environment virtualenv packages
Oct 06 '13 at 16:12
source share
5 answers

This may be legal use of PYTHONPATH , an environment variable that virtualenv does not apply to, which uses the same syntax as the PATH environment variable, in bash PYTHONPATH=/usr/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages in .bashrc or similar. If you follow this path,

  • You do not need to talk about this virtual environment at all, it will not try to change it.

  • No repetitions are required, and

  • This will continue wherever he goes ( pip install always uses / usr / local / lib / python2.7 / dist-packages / for my Ubuntu) if you install them outside of your virtual environment. If you install them from your virtual environment (while it is activated), then, of course, it will be placed in a virtual environment.

+13
Oct 06 '13 at 17:13
source share

I just look around virtualenv, but there seems to be an easier way than mentioned so far.

  • Since virtualenv 1.7 non-site packages were the default. Therefore, using the --system-site-packages flag for virtualenv is all that is needed to receive dist packages in your path - if you use the modified virtualenv sent by Ubuntu. ( This answer and this give some useful history). I tested this and it works.

    $ virtualenv --system-site-packages .

  • I agree with Thomas here - I don't see any actions required in virtualenv to see the effect of updates in dist packages.

  • After testing this with python setup.py install , it (again, as Thomas said) still goes for dist packages. You can change this by creating your own python, but a little extreme.

+6
Jul 27 '15 at 16:49
source share

PYTHONPATH works for me.

 vim ~/.bashrc 

add this line below:

 export PYTHONPATH=$PYTHONPATH:/usr/lib/python2.7/dist-packages:/usr/local/lib/python2.7/dist-packages source ~/.bashrc 
+4
Mar 27 '14 at 3:31
source share

What you want to achieve essentially adds a specific folder ( dist-packages ) to the Python search path. You have several options for this:

  • Use the path configuration configuration ( .pth ), entries will be added to the system path.
  • Change PYTHONPATH (entries from it go to the beginning of the system path).
  • Modify sys.path directly from your Python script, i.e. add the necessary folders to it.

I think that for this specific case (include the dist-packages global folder) the third option is better, because with the first parameter you need to create a .pth file for each virtual user you will work with (with some external shell script?). It is easy to forget when you distribute your package. The second option requires setting the runtime (add envvar), which, again, is easy to skip.

And only the third option does not require any preconditions in the configuration mode or runtime and can be distributed without problems (in a system of the same type, of course).

You can use this function:

 def enable_global_distpackages(): import sys sys.path.append('/usr/lib/python2.7/dist-packages') sys.path.append('/usr/local/lib/python2.7/dist-packages') 

And then in the __init__.py file of your package:

 enable_global_distpackages() 
0
Jun 20 '14 at 12:37
source share

In the site-packages directory, create the dist.pth file In the dist.path file, enter the following: ../dist-packages

enter image description here

Now disconnect and activate your virtualenv. You must be installed.

0
Dec 16 '17 at 5:49
source share



All Articles