How to read file during module import?

Jeremy jlconlin at gmail.com
Fri Apr 9 17:04:59 EDT 2010


I have a module that, when loaded, reads and parses a supporting
file.  The supporting file contains all the data for the module and
the function that reads/parses the file sets up the data structure for
the module.

How can I locate the file during the import statement.  The supporting
file is located in the same directory as the module, but when I import
I get a No such file or directory error.  I could hard code the path
to the filename, but that would make it only work on my machine.

A related question: Can I parse the data once and keep it somewhere
instead of reading the supporting file every time?  I tried pickling
but that wouldn't work because I have custom classes.  (Either that or
I just don't know how to pickle—this is a highly probable event.)

Thanks,
Jeremy



More information about the Python-list mailing list