6

I need to add a new directory location to my PYTHONPATH, but the problem is I'm on a clean, newly-installed system (Linux) where no PYTHONPATH has yet been defined. I've read about and used PYTHONPATH and I thought I understood it quite well, but I do not know what's happening when no PYTHONPATH yet exists.

I can't append to something that doesn't exist, but I want all important libraries currently found to still work, so being cautious, from within Python I did print str(sys.path) to get all the standard values. Then I defined an env-variable for PYTHONPATH including all the nodes I had just found, plus my new directory. But wow did a lot of stuff stop working! Python is so messed up with the new env-variable that I had to remove it, at which point everything worked again. With the bad PYTHONPATH the system was so confused it couldn't even find an error message to display when an incorrect command was typed in at the prompt.

My problem is not something simple like a missing colon, or using semi-colons when I should use colons; I checked. Also my new directory doesn't cause the problem because even without the new node the problems still occur. So can anyone explain why this approach doesn't work?

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Below I provide extra details as requested, but one need not read any futher, I think the problem is fixed. The explanation that the nodes listed in PYTHONPATH do not override all "standard" nodes but rather become new, additional entries (prepended I believe, so one can control what comes first) was the key.

Starting from scratch, defining no PYTHONHOME or PYTHONPATH, results in this from within Python:

print ':'.join(sys.path)      
:/usr/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PIL:/usr/lib/python2.7/dist-packages/gst-0.10:/usr/lib/python2.7/dist-packages/gtk-2.0:/usr/lib/python2.7/dist-packages/ubuntu-sso-client   

Using this as a PYTHONPATH (i.e., defining an env-variable before invoking Python), results in a very poorly functioning command prompt, even without explicitly using Python. For example:

$> export PYTHONPATH='/usr/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PIL:/usr/lib/python2.7/dist-packages/gst-0.10:/usr/lib/python2.7/dist-packages/gtk-2.0:/usr/lib/python2.7/dist-packages/ubuntu-sso-client'

$> echo $PYTHONPATH        
/usr/lib/python2.7:/usr/lib/python2.7/plat-linux2:/usr/lib/python2.7/lib-tk:/usr/lib/python2.7/lib-old:/usr/lib/python2.7/lib-dynload:/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages/PIL:/usr/lib/python2.7/dist-packages/gst-0.10:/usr/lib/python2.7/dist-packages/gtk-2.0:/usr/lib/python2.7/dist-packages/ubuntu-sso-client

$> IntentionalBadCommand       
Fatal Python error: Py_Initialize: Unable to get the locale encoding    
 File "/usr/lib/python2.7/encodings/__init__.py", line 123    
raise CodecRegistryError,\    
                        ^
SyntaxError: invalid syntax       
Aborted

The mistake was thinking that PYTHONPATH needs to contain the whole universe of everything needed. Yes, I did RTFM before posting, but I guess I missed the significance of the beginning word "Augment". So taking the advice that not everything needs to be explicitly specified -- that one can just specify the extra additions one wants, I tried:

$> export PYTHONPATH=/usr/lib/python2.7/dist-packages/postgresql-pkg

$> echo $PYTHONPATH         
/usr/lib/python2.7/dist-packages/postgresql-pkg

$> IntentionalBadCommand       
IntentionalBadCommand: command not found

So it seems to be working, though I haven't yet tried to use the postgresql package mentioned above. Still it's a little mysterious why prepending an abundance of unnecessary nodes to the PYTHONPATH would make things break as badly as it did, especially since I got the entries from what should be a reliable source: sys.path.

But anyway, it's probably solved, so THANKS!

  • 2
    For future reference, you can just write `---` instead of a row of `%` characters, and it'll actually insert a horizontal line with space around it. – abarnert Jan 25 '13 at 22:52

2 Answers2

7

It's not clear what your problem could be, but note that you don't need to add the default value of sys.path to your PYTHONPATH variable. The directories you put in PYTHONPATH are additional directories to search; the system default is appended to your PYTHONPATH. In other words, roughly speaking:

sys.path = ":".split( os.environ['PYTHONPATH'] ) + sys.path

Showing the exact value of PYTHONPATH and the resultant errors would help us determine the problem.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • In other words, PYTHONPATH is optional. In most cases you should not have to define it. Thus, the fresh Linux install doesn't have it either. – Keith Jan 25 '13 at 20:35
  • Please see new section added to question, following string of percent signs (space is limited for comments). Thanks very much for the help. – Will Zimmerman Jan 25 '13 at 21:07
  • +1 @Andrew for the source link, always helpful when the documentation isn't quite clear enough. – abarnert Jan 25 '13 at 22:52
1

On Unix Systems, it should translate to /usr/local/lib/python** where ** is Python version...Like 2.7 and so on...

Your answer actually lies in the definition of PYTHONPATH and PYTHONHOME variables.

http://docs.python.org/2/using/cmdline.html#envvar-PYTHONHOME

The default search path is installation dependent, but generally begins with prefix/lib/pythonversion (see PYTHONHOME above). It is always appended to PYTHONPATH.

I would suggest you also try this

import sys    
sys.path.append('Directory you wanna add to the path')

Hope this helps.

Guddu
  • 1,588
  • 4
  • 25
  • 53
  • 1
    Which Unix systems is it `/usr/local/PythonXY`? On most Unix and Unix-like systems, including linux, it'll be in `/usr/local/lib/pythonX.Y` or sometimes `/usr/lib/pythonX.Y`, or `/some/other/prefix/lib/pythonX.Y`—but it's always under `lib`, with a lowercase `p`, and with a dot between the version numbers. And the only common exception I can think of is OS X, with a framework build, where it's something like `/Library/PythonX.Y` or `/Library/Frameworks/Python.framework/Versions/X.Y/lib/pythonX.Y`. – abarnert Jan 25 '13 at 19:53
  • Thanks for down-voting my answer right away without offering me a chance to make amends. You have earned yourself another credit. You are right. I have corrected the Typo....The real essence of my answer was in the official page that I guided him to. The installation paths are configurable and not written in hard. – Guddu Jan 25 '13 at 20:25
  • 2
    You realize downvotes can be undone if you fix the answer, right? Also, remember that votes are about how useful an answer is, nothing else. If an answer is incorrect and misleading to future readers, it should have a low score. If the same answer is later improved so that it's correct and useful, it should have a high score. Neither one is a reflection on you as a programmer or a human being, only on the merits of the answer. – abarnert Jan 25 '13 at 20:28
  • Thanks Abarnert...Wasn't sure of the undoing part. Another upvote for you....As of today, I stand banned for asking questions.....To be honest, I am not sure why. My answer to this post itself doesn't leave a impression but if you look at my other questions/answers you will understand that I am still doing a fair job at SO.....Trying to remove my ban somehow. Will keep trying and contributing in the best way possible and will try to keep an eye on the typos etc... – Guddu Jan 25 '13 at 20:46
  • Please see new section added to question, following string of percent signs (space is limited for comments). Thanks very much for the help. – Will Zimmerman Jan 25 '13 at 21:08
  • @Guddu: That sounds like something you might want to search (or ask about) on [meta](http://meta.stackoverflow.com). (I still haven't figured out how half the stuff works here.) – abarnert Jan 25 '13 at 22:51