Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Virtualenv on Ubuntu with no site-packages

Tags:

I've been using virtualenv lately while developing in python. I like the idea of a segregated development environment using the --no-site-packages option, but doing this while developing a PyGTK app can be a bit tricky. The PyGTK modules are installed on Ubuntu by default, and I would like to make a virtualenv (with --no-site-packages) aware of specific modules that are located elsewhere on the system.

What's the best way to do this? Or should I just suck it up and drop the --no-site-packages option?

like image 354
Jeremy Cantrell Avatar asked Oct 30 '08 04:10

Jeremy Cantrell


People also ask

Is VENV the same as virtualenv?

These are almost completely interchangeable, the difference being that virtualenv supports older python versions and has a few more minor unique features, while venv is in the standard library.

Where are packages installed in virtualenv?

Similar to your system's Python installation, the packages are stored inside lib/python2. */site-packages/ directory.

Is virtualenv a package?

virtualenv is used to manage Python packages for different projects. Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects. You can install virtualenv using pip.

Does virtualenv inherit packages?

If you build with virtualenv --system-site-packages ENV, your virtual environment will inherit packages from /usr/lib/python2. 7/site-packages (or wherever your global site-packages directory is).


2 Answers

$ virtualenv --no-site-packages --python=/usr/bin/python2.6 myvirtualenv $ cd myvirtualenv $ source bin/activate $ cd lib/python2.6/ $ ln -s /usr/lib/pymodules/python2.6/gtk-2.0/  $ ln -s /usr/lib/pymodules/python2.6/pygtk.pth  $ ln -s /usr/lib/pymodules/python2.6/pygtk.py  $ ln -s /usr/lib/pymodules/python2.6/cairo/ $ python >>> import pygtk >>> import gtk 
like image 81
iElectric Avatar answered Oct 11 '22 18:10

iElectric


One way is to add the paths to your code using sys.path.

import sys  sys.path.append(somepath) 

Another way is to use site, which processes .pth files in addition to adding to sys.path.

import site  site.addsitedir(sitedir, known_paths=None) 

https://docs.python.org/library/site.html

But you probably don't want to add this to all your related code.

I've seen mention of sitecustomize.py being used to perform something like this, but after some testing I couldn't get it to work as might be expected.

Here it mentions that auto-import of sitecustomize.py ended in 2.5, if your not on 2.5 try it out. (just add one of the path add methods above to the file and drop it in the directory your program is run) A work around method is mentioned in the post for users of 2.5 and up.

http://code.activestate.com/recipes/552729/

like image 24
monkut Avatar answered Oct 11 '22 16:10

monkut