My python's breath smells like ImportError
October 6, 2006 8:14 AM
Subscribe
Referencing roll-your-own Python modules in other Python applications. What's my deal?
So at my newish job I am consolidating a bunch of redundant code into a core library which is referenced by various CGI scripts. The problem comes when I try importing these libraries. For sake of demonstration, my directory structure looks like this:
(a) /home/me/html/bin/
(b) /home/me/html/bin/edu/school/library/utilities/
(c) /home/me/html/bin/appname/
where (a) is the base CGI directory, (b) is the directory in which the library lives and (c) is where the application importing a module from the library lives.
When an application importing a module in (b) resides in (c) I get an ImportError. When an application importing a module in (b) resides in (a) the import comes off without a hitch. AFAIK, I've isolated every variable such that the only difference between the two cases is the directory in which the calling application lives.
While I can kludge things around by putting all my CGI apps in /bin, I'd prefer to organize things a bit better. Thus I invoke the MeFi hive mind to provide me the undoubtedly easy solution for importing modules from an arbitrary location in the file system.
My final salvos:
1. __init__.py lives in every directory from bin on down.
2. sys.path includes /home/me/html/bin/edu
posted by Fezboy! to computers & internet (6 comments total)
By default Python is looking in "." and in /usr/share/lib/python/VERSION/site-packages (or something similar). If you put libraries somewhere else, you have to tell it where else to look.
posted by metajack at 8:21 AM on October 6, 2006