I tried adding the directory /home/myname/bin to my PYTHONPATH by inserting the following into my .bashrc:
if [ -d ~/bin ]; then export PYTHONPATH=$PYTHONPATH:/home/myname/bin
fiUnfortunately this seems to add my home directory to Python's search path, as seen by running the following commands in Python:
>>> import sys
>>> sys.path
['', '/home/myname', '/home/myname/bin', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages']Note that echo $PYTHONPATH just returns :/home/myname/bin.
1 Answer
The problem is that you are including the existing value of PYTHONPATH. That variable, however, is not set, so it evaluates to the empty string. For some reason, that seems to make python include your $HOME dir. Presumably, that is the default directory:
$ python -c 'import sys; print sys.path'
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/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/PILcompat', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/wx-3.0-gtk2']However, if I set PYTHONPATH to the empty string, my $HOME is included:
$ PYTHONPATH=""; python -c 'import sys; print sys.path'
['', '/home/terdon', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/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/PILcompat', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/wx-3.0-gtk2']So, when you include the current value of PYTHONPATH when setting it, that's the behavior you get:
$ PYTHONPATH="$PYTHONPATH:$HOME/bin" python -c 'import sys; print sys.path'
['', '/home/terdon', '/home/terdon/bin', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/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/PILcompat', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/wx-3.0-gtk2']The correct way to set it is by only giving it the directories you want to add:
$ PYTHONPATH="$HOME/bin" python -c 'import sys; print sys.path'
['', '/home/terdon/bin', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/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/PILcompat', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/wx-3.0-gtk2']Or, to be on the safe side in case it ever is defined, check whether it is empty and act accordingly:
if [ -d ~/bin ]; then [ -z "$PYTHONPATH" ] && export PYTHONPATH="/home/myname/bin" || export PYTHONPATH="$PYTHONPATH:/home/myname/bin"
fi