Sunday, 18 February, 2007
Python and path resolution under Windows
A new project has me diving into Python. Python is an interesting and somewhat quirky language: quirky in that it does some things much differently than how I've seen them done in other programming languages. With just a couple of days' experience, I can say that I like the model more than I like PHP, mostly because Python shows more forethought--design--whereas PHP looks like it started as a clever hack that evolved. I'll be the first to admit that this is a totally subjective opinion that's based on very little familiarity with either language.
I ran into an interesting problem today when trying to get the urlgrabber package working. I downloaded and installed the Python module, ran the unit tests, and was told that everything worked. But when I tried to run the example program, called urlgrabber, I kept getting an error message trying to import
urlgrabber.grabber. This was very odd because the sample programs had no problem importing that module.
30 minutes searching the Web didn't reveal a solution. It finally dawned on me that the problem might be that there might be a name resolution conflict. The program called urlgrabber was trying to reference a module called urlgrabber. This probably isn't a problem under Linux, but under Windows it appears that Python was looking in the current working directory for the urlgrabber module. What it found instead was the urlgrabber program itself. It then searched for grabber in that directory and didn't find it.
I confirmed my suspicion by renaming urlgrabber.py to xxx.py. The program works fine now.
This all makes sense. Windows always searches the current working directory first when looking for programs, DLLs, and other things. Only after that does it start searching the directories identified in the PATH and in other places. Linux, on the other hand, does things differently. In particular, you can configure Linux so that it specifically does not search the current working directory. This is very common, in fact, when running as root.
Problem solved. For now. On to the next one.