Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the PYTHONPATH when there is no PYTHONPATH?

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!

like image 530
Will Zimmerman Avatar asked Jan 25 '13 19:01

Will Zimmerman


1 Answers

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.

like image 190
chepner Avatar answered Sep 19 '22 00:09

chepner