Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether virtualenv was created with '--no-site-packages'?

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.

like image 608
elimisteve Avatar asked Jan 16 '12 22:01

elimisteve


2 Answers

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
like image 104
Rob Wouters Avatar answered Sep 22 '22 13:09

Rob Wouters


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'>
like image 34
ThiefMaster Avatar answered Sep 23 '22 13:09

ThiefMaster