I need to add a new folder to my PYTHONPATH , but the problem is that I am on a clean, recently installed system (Linux) where no PYTHONPATH yet to be defined. I read and used PYTHONPATH , and I thought I understood it well, but I donβt know what happens when no PYTHONPATH exists yet.
I cannot add to something that does not exist, but I want all the important libraries currently still working, so fearing from within Python I did print str(sys.path) to get all the standard values. Then I defined env -variable for PYTHONPATH , including all the nodes I found, plus my new directory. But wow, a lot of things have stopped working! Python was so confused with the new env variable that I had to remove it, after which everything worked again. With poor PYTHONPATH system was so confused that it could not even find an error message to display when an incorrect command was entered in the prompt.
My problem is not as simple as the missing colon, or the use of half-columns, when I should use the colon; I checked. Also, my new directory does not cause a problem, because even without a new node, problems still arise. So can someone explain why this approach does not work?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Below I provide additional data on request, but no one should read further, I think the problem is fixed. The explanation that the nodes listed in PYTHONPATH do not override all the "standard" nodes, but rather become new, the key are additional entries (for example, I believe that you can control what comes first).
Starting from scratch without defining PYTHONHOME or PYTHONPATH, this is obtained 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 PYTHONPATH (i.e. defining an env variable before calling Python) results in a very malfunctioning command line, even without using Python explicitly. For instance:
$> 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 that PYTHONPATH should contain the whole universe of everything needed. Yes, I did RTFM before publishing, but I probably missed the meaning of the initial word "Augment". Therefore, taking the advice that not everything should be explicitly stated - that you can simply specify the additional additions you want, 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 looks like this works, although I have not yet tried to use the postgresql package mentioned above. However, this is a bit cryptic, because adding an excess of unnecessary nodes to PYTHONPATH will make things as bad as they were, especially since I got entries from what should be a reliable source: sys.path.
But anyway, he probably decided, so THANKS!