简体   繁体   中英

Virtualenv Includes Global Packages / How do I clear my PYTHONPATH?

I'm running a Virtualenv like this:

$ virtualenv --no-site-packages venv
New python executable in .../venv/bin/python
Installing setuptools, pip, wheel...done.
$ source venv/bin/activate

But when I pip freeze -l , I don't get anything. So I double-checked my PYTHONPATH :

$ python
import sys
for i in sys.path:
...     print i

Which gave this output:

/home/...
/usr/local/lib/python2.7/site-packages
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/site-packages
/usr/lib/python2.7/dist-packages
/home/.../venv/lib/python2.7
/home/.../venv/lib/python2.7/plat-x86_64-linux-gnu
/home/.../venv/lib/python2.7/lib-tk
/home/.../venv/lib/python2.7/lib-old
/home/.../venv/lib/python2.7/lib-dynload
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/home/.../venv/local/lib/python2.7/dist-packages
/home/.../venv/lib/python2.7/site-packages
/home/.../venv/local/lib/python2.7/dist-packages

In the directories, I used ... to denote the path to my project/working directory.

It seems to me that what I really need to do is ensure that in my virtual environment, the /usr/ paths don't show up. But how can I do that? How do I clear these extraneous paths?

I don't know if this is a good fix, but I found a few statements in my .bashrc:

export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/dist-packages
export PYTHONPATH=$PYTHONPATH:/usr/lib/python2.7/site-packages
export PYTHONPATH=$PYTHONPATH:/usr/lib/python2.7/dist-packages

Which I commented out, and then those paths no longer showed up in sys.path . Now my virtualenv is working as desired, though I'd be curious to understand why this actually fixes the problem.

basrc is used to configure your environment when you boot the system/start a terminal session. So for example you don't have to manually configure things (like pythonpath, or maybe some convenient command line aliases) every time you start a new terminal session. Every time you started a new session, you were recreating the PYTHONPATH environment variable until of course you commented them out from the bashrc, at which point once you unset the PYTHONPATH it remained empty. So yes, it's a good fix. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM