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

前端 未结 3 1773
半阙折子戏
半阙折子戏 2020-12-31 04:29

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

3条回答
  •  天涯浪人
    2020-12-31 04:46

    @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:

    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
    

提交回复
热议问题