Wednesday, October 21, 2009

Quirky Python Behaviour on Ubuntu

So my good friend AD is messing around with Python these days. You know, basic variables and loops and stuff. So he does some stuff with strings and naively names the file string.py and saves it in his home folder.

Done with this brainwork for the day, he fires up rhythmbox to listen to some music. The busy cursor shows and nothing else comes up. Since I'm the only linux 'expert' in sight he turns to me for help.

I advise him to run rhythmbox from the command prompt and tell me what the error messages are. He does that and receives a segmentation fault with random garbage from the python exercises he was doing earlier.

What was happening here was that python was treating the string.py in as the default string module. Or maybe it's a rhythmbox thing. Or an Ubuntu thing. In anycase, it seems like a stupid thing to give precedence to random stuff lying around in your /~. Is this a feature or a bug?

2 comments:

  1. It's not random. It's just expected behavious. Remember that there are reserved keywords in C? Well python gives you the liberty to override things. You can name your module the same as one which kind by found in your python path. Current folder (.) takes highest precedence, there by giving you an opportunity to override modules.

    This was just a bad choice on his part. Then there is something else which is fucked up either on the part of Ubuntu of something but certainly not python which being why is home a part of env variable $PYTHONPATH?

    If someone enabled this purposefully then it's bad practice. One can check path where python looks for modules by printing sys.path in python. e.g.

    vyvyan@athena ~ $ python
    Python 2.6.2 (r262:71600, Oct 14 2009, 02:14:10)
    [GCC 4.3.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sys
    >>> print sys.path
    ['', '/usr/lib/python2.6/site-packages/SQLAlchemy-0.5.5-py2.6.egg', '/usr/lib/python2.6/site-packages/SQLObject-0.11.0-py2.6.egg', '/usr/lib/python2.6/site-packages/FormEncode-1.2.2-py2.6.egg', '/usr/lib/python2.6/site-packages/twitter-1.2.1-py2.6.egg', '/usr/lib/python2.6/site-packages/python_dateutil-1.4.1-py2.6.egg', '/usr/lib/python2.6/site-packages/simplejson-2.0.9-py2.6-linux-i686.egg', '/usr/lib/python2.6/site-packages/BitTorrent_bencode-5.0.8-py2.6.egg', '/usr/lib/python2.6/site-packages/IMDbPY-4.2-py2.6-linux-i686.egg', '/usr/lib/python2.6/site-packages/Elixir-0.6.1-py2.6.egg', '/usr/lib/python2.6/site-packages/yolk-0.4.1-py2.6.egg', '/usr/lib/python2.6/site-packages/yolk_portage-0.1-py2.6.egg', '/usr/lib/python2.6/site-packages/pip-0.4-py2.6.egg', '/usr/lib/python26.zip', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/gst-0.10', '/usr/lib/python2.6/site-packages/gtk-2.0', '/usr/lib/portage/pym']

    ReplyDelete
  2. @Kushwaha - I know, I know. I think the only 'quirky' part here is that home is by default in pythonpath. It's most probably an ubuntu or rhythmbox thing, as I mentioned. I don't think home should by default be in pythonpath.

    ReplyDelete