Is my virtual environment (python) causing my PYTHONPATH to break?
In my /etc/profile, I set PYTHONPATH as something.
But when I source myvirtual-env
And th开发者_如何学Cen do this in python:
>>> import sys
>>> print sys.path
I don't see my paths anywhere.
That's the point of the virtualenv. It doesn't inherit from the rest of your setup. If you want a PYTHONPATH, you need to explicitly set one.
This djangousers post is probably helpful, you want to use virtualenvwrapper to solve this problem.
More info in this other SO post on a similar problem.
I don't observe the problem with Python 2.7, virtualenv 1.7.1.2 on Windows XP and I suspect Paul McMillan's answer is wrong.
# PYTHONPATH not set
# output from python -c "import sys; print sys.path" (edited for clarity)
'',
'C:\\Program Files\\python\\2.7\\lib\\site-packages\\pip-1.1-py2.7.egg',
'C:\\WINDOWS\\system32\\python27.zip',
'C:\\Program Files\\python\\2.7\\DLLs',
'C:\\Program Files\\python\\2.7\\lib',
'C:\\Program Files\\python\\2.7\\lib\\plat-win',
'C:\\Program Files\\python\\2.7\\lib\\lib-tk',
'C:\\Program Files\\python\\2.7',
'C:\\Program Files\\python\\2.7\\lib\\site-packages',
'C:\\Program Files\\python\\2.7\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-info'
# PYTHONPATH not set, virtualenv activated
# output from python -c "import sys; print sys.path" (edited for clarity)
'',
'c:\\python\\virtualenv\\2.7\\lib\\site-packages\\distribute-0.6.24-py2.7.egg',
'c:\\python\\virtualenv\\2.7\\lib\\site-packages\\pip-1.1-py2.7.egg',
'C:\\WINDOWS\\system32\\python27.zip',
'c:\\python\\virtualenv\\2.7\\DLLs',
'c:\\python\\virtualenv\\2.7\\lib',
'c:\\python\\virtualenv\\2.7\\lib\\plat-win',
'c:\\python\\virtualenv\\2.7\\lib\\lib-tk',
'c:\\python\\virtualenv\\2.7\\Scripts',
'C:\\Program Files\\python\\2.7\\Lib',
'C:\\Program Files\\python\\2.7\\DLLs',
'C:\\Program Files\\python\\2.7\\Lib\\lib-tk',
'c:\\python\\virtualenv\\2.7',
'c:\\python\\virtualenv\\2.7\\lib\\site-packages'
# PYTHONPATH set to c:\pythonpath_sample_dir
# output from python -c "import sys; print sys.path" (edited for clarity)
''
'C:\\Program Files\\python\\2.7\\lib\\site-packages\\pip-1.1-py2.7.egg'
'c:\\pythonpath_sample_dir' <--- value from PYTHONPATH
'C:\\WINDOWS\\system32\\python27.zip'
'C:\\Program Files\\python\\2.7\\DLLs'
'C:\\Program Files\\python\\2.7\\lib'
'C:\\Program Files\\python\\2.7\\lib\\plat-win'
'C:\\Program Files\\python\\2.7\\lib\\lib-tk'
'C:\\Program Files\\python\\2.7'
'C:\\Program Files\\python\\2.7\\lib\\site-packages'
'C:\\Program Files\\python\\2.7\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-info'
# PYTHONPATH set to c:\pythonpath_sample_dir, virtualenv activated
# output from python -c "import sys; print sys.path" (edited for clarity)
''
'c:\\python\\virtualenv\\2.7\\lib\\site-packages\\distribute-0.6.24-py2.7.egg'
'c:\\python\\virtualenv\\2.7\\lib\\site-packages\\pip-1.1-py2.7.egg'
'c:\\pythonpath_sample_dir' <--- value from PYTHONPATH
'C:\\WINDOWS\\system32\\python27.zip'
'c:\\python\\virtualenv\\2.7\\DLLs'
'c:\\python\\virtualenv\\2.7\\lib'
'c:\\python\\virtualenv\\2.7\\lib\\plat-win'
'c:\\python\\virtualenv\\2.7\\lib\\lib-tk'
'c:\\python\\virtualenv\\2.7\\Scripts'
'C:\\Program Files\\python\\2.7\\Lib'
'C:\\Program Files\\python\\2.7\\DLLs'
'C:\\Program Files\\python\\2.7\\Lib\\lib-tk'
'c:\\python\\virtualenv\\2.7'
'c:\\python\\virtualenv\\2.7\\lib\\site-packages'
Also python virtualenv: why can I still import old modules in clean/new virtualenv seems to confirm PYTHONPATH
is being used to construct sys.path
also when virtual environment is activated.
精彩评论