(relative) import trouble, sometimes it works, sometimes it doesn't ...

Stef Mientki stef.mientki at gmail.com
Sat Oct 18 04:52:04 EDT 2008


hello,

I'm running Python 2.5 and want my programs to run at least under 
Windows and Linux (preferable also Mac).
So I guess I should always use relative paths.

 From most modules I can call a global function,
that should import a dictionary from path deeper than the module itself.
The import is done in the global function.
In that global function, I get the modules path by

  SourceFile = sys._getframe(1).f_code.co_filename

now to be sure to succeed the import (at least I was thinking this would 
always be successful  :-(
I need to
1- add the path of the module to be imported to sys.path
(or do a dot import)
2- keep track of already done imports, to give a reload the next time
(or maybe always do an import followed by an reload ?)

Now what I don't understand is what relative path should I use in 1:
- relative to the main application
- relative to the working directory were I started the application
- relative to the current working directory
- relative to the module that is doing the import
- relative to the module that called the global function

I would be pleased if someone could enlighten me,
because this information is hard to find.

thanks,
Stef Mientki



More information about the Python-list mailing list