PYTHONPATH - dynamic for different python installs?
On my CentOS5 server, I have both Python2.4 and 2.6 (2.4 is required for yum to work). I'm not sure what happened, but suddenly the system gets very confused every time I try to run a file whose modules are loaded into the 2.4 site-packages directory. I checked the PYTHONPATH/sys.path and it looks like everything was overwritten with 2.6 environment data instead.
It didn't used to do this. I simply declared /usr/bin/python or /usr/bin/python26 in the shebang statement at the beginning of the script and it always found the correct modules just fine.
Is there a way for the PYTHONPATH variable to be dynamic and load different paths based on which python interpreter is running?
Otherwise I'm going to have to manually edit the path in every application, which seems like overkill.
It started after installing web.py (which I love, by the way).
Traceback: As someone commented below, I changed the shebang to be #!/usr/bin/env python for this program:
Traceback (most recent call last):
File "/usr/bin/linkchecker", line 24, in ?
import codecs
File "/usr/lib/python2.6/codecs.py", line 268
return (b"", 0)
^
Another example, trying to use yum:
Traceback (most recent call last):
File "/usr/bin/yum", line 5, in ?
import yum
File "/usr/lib/python2.4/site-packages/yum/__init__.py", line 21, in ?
import os
File "/usr/lib/python2.6/os.py", line 758
bs = b""
^
I've noticed a couple programs not confou开发者_开发问答nded by the b"" syntax, and all of them are programs meant to use 2.4 that are for some reason using 2.6. If I try to make the program use the 2.6 interpreter it is able to understand that syntax, but then can't find any of the other modules (which are in the 2.4 site-packages directory).
I don't know what that syntax is, as they were written into modules which I got from sourceforge, however they were working last week. I am not sure what changed.
Thanks, Tom
First of all, use virtualenv to isolate packages for multiple Python installations. Most of your problems will go away immediately.
Second, as Ibp has recommended in his answer, change the shebang line to use the "currently active" python binary so that it will work across multiple interpreters.
Instead of using the shebang (first bytes of the file)
#!/usr/bin/python
use the shebang
#!/usr/bin/env python
Edit: I second Noufal's suggestion to use virtualenv.
精彩评论