19

Sometimes I get errors that I suspect are the result of my Django app using globally installed Python modules/Django apps instead of those within its virtualenv.

Is there a way to check whether my app's virtualenv was created with '--no-site-packages' without having to delete it, then re-create it as follows?

deactivate
rmvirtualenv my_env
mkvirtualenv my_env --no-site-packages
workon my_env
pip install -r requirements.txt

Surely there must be a better way! Thanks.

elimisteve
  • 1,771
  • 1
  • 18
  • 22

3 Answers3

23

There's a file in <env>/lib/pythonX.X/ called no-global-site-packages.txt when you create a virtual environment with --no-site-packages.

Just tried this with virtualenv 1.7:

% virtualenv --no-site-packages env.without
% virtualenv --system-site-packages env.with

% find env.without | sed 's/env.without//' > files.without
% find env.with | sed 's/env.with//' > files.with

% diff files.with*
230a231
> /lib/python3.2/no-global-site-packages.txt
Rob Wouters
  • 15,797
  • 3
  • 42
  • 36
6

An easy way is opening the interactive python shell and executing import somemodule; print somemodule and then check the path from where that module was imported.

>>> import flask; print flask
<module 'flask' from '/home/xxx/dev/xxx/env/lib/python2.7/site-packages/flask/__init__.pyc'>

vs.

>>> import flask; print flask
<module 'flask' from '/usr/lib64/python2.7/site-packages/flask/__init__.pyc'>
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • This is valid only if you have the same module installed in both places. You can't really try to import both at the same time (unless you change the pythonpath) – Laur Ivan Jan 17 '12 at 13:15
  • If it's not, you'll get an `ImportError` - depending on where you get it you know that it's only installed at location X. – ThiefMaster Jan 17 '12 at 13:25
  • True if you have the module installed in site-packages. But then you have to select a module which you _know_ it's there and in fact check for `ImportError` – Laur Ivan Jan 17 '12 at 15:09
  • It was an example. Also, `__future__` *always* points to the python's lib folder even if a virtualenv is active. – ThiefMaster Sep 05 '13 at 14:42
3

@Rob's solution is valid for newer versions, I've looked into the code :).

If you have an old one (like my 1.4.5), you can check the python path. If you have the default "site-packages" directory in the path (e.g. /usr/lib/python/site-packages), then your virtualenv was created with site-packages.

You can check it out from something like:

import sys
for p in sys.path:
   if p.find("site-packages") >= 0:
     print (p)

If you had --no-site-packages, all your paths would be like:

/home/user/virtualenv/myenv/lib/python2.6/site-packages/distribute-0.6.10-py2.6.egg
/home/user/virtualenv/myenv/lib/python2.6/site-packages/pip-1.0.2-py2.6.egg
/home/user/virtualenv/myenv/lib/python2.6/site-packages

Otherwise, you'll have something like:

/home/user/virtualenv/myenv/lib/python2.6/site-packages/distribute-0.6.10-py2.6.egg
/home/user/virtualenv/myenv/lib/python2.6/site-packages/pip-1.0.2-py2.6.egg
/home/user/virtualenv/myenv/lib/python2.6/site-packages
/usr/local/lib/python2.6/site-packages
Laur Ivan
  • 4,117
  • 3
  • 38
  • 62