devork

E pur si muove

Today's WTF: sys.path in python != sys.path in ipython

Thursday, August 17, 2006

Spot the current directory (<snip>/build/lib.linux-i686-2.4) in the path:

<snip>/build/lib.linux-i686-2.4$ echo $PYTHONPATH
/home/flub/lib/python:/home/flub/lib/python2.4:

<snip>/build/lib.linux-i686-2.4$ python
Python 2.4.3 (#2, Apr 27 2006, 14:43:58)
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '<snip>/build/lib.linux-i686-2.4', '/home/flub/lib/python', '/home/flub/lib/python2.4', '/usr/lib/python24.zip', '/usr/lib/python2.4', '/usr/lib/python2.4/plat-linux2', '/usr/lib/python2.4/lib-tk', '/usr/lib/python2.4/lib-dynload', '/usr/lib/python2.4/site-packages', '/usr/lib/site-python']
>>> Ctrl-D

<snip>/build/lib.linux-i686-2.4/$ ipython
In [1]:import sys
In [2]:sys.path
Out[2]:
['',
'/usr/bin',
'/home/flub/lib/python',
'/home/flub/lib/python2.4',
'/usr/lib/python24.zip',
'/usr/lib/python2.4',
'/usr/lib/python2.4/plat-linux2',
'/usr/lib/python2.4/lib-tk',
'/usr/lib/python2.4/lib-dynload',
'<snip>/build/lib.linux-i686-2.4',
'/usr/lib/site-python',
'/home/flub/.ipython',
'/usr/lib/python2.4/site-packages/IPython/Extensions']
In [3]: Ctrl-D
>snip>/build/lib.linux-i686-2.4$

This seriously screws up my interactive sessions with ipython since the module in /usr/lib gets loaded before my own modified one. Why does ipython think it is a good idea to do it that way?

Since we're at the subject, notice the PYTHONPATH environment variable. Why does just the existence of this variable -- what it is set to does actually not matter at all -- make the current directory show up in the path. Also what's the point of the empty ('') string at the beginning of the path? What does that do?

Lots of questions, I know, but I haven't found any of the answers anywhere so far. Maybe I have just looked to hard, but I'm very intrigued.

Thursday, August 17, 2006 | Labels: |

1 comments:

Harbort said...

Hi,

first, the '' means 'look for modules in the current directory'. This is useful for an interactive session.

Then, for ipython, I think the problem is the evaluation order of the files and environment variable ... in my case, I get the eggs before everything else, then PYTHONSTARTUP and then the other directories. As for the presence of the current directory explicitly in the sys.path, I don't know, I never saw that and still don't with Python 2.4.4c0.

New comments are not allowed.

Subscribe to: Post Comments (Atom)