Absolute Imports and PYTHONPATH

Richard Cooper python-list at richardcooper.net
Wed May 5 17:48:42 EDT 2004


I can't believe I've just been bitten by this again!

I'm part of a team writing an application in python. We have an fairly 
extensive package structure and like sensible pythoneers we use 
absolute imports throughout. The top level of our package is in 
PYTHONPATH so that all the absolute imports can work.

This is all fine and dandy until I start playing with two different 
versions of the code (working on 2 CVS branches for example). Whenever 
I do this I inevitably forget to change PYTHONPATH at some point and 
waste an hour or so debugging a problem that occurred because the 
__main__ module from one tree is now importing from the other. This has 
happened to me a few times now and is really irritating.

Am I missing an obvious non-hacktastic way to avoid this problem?

If not would it be possible to fix this problem in a future python? I'm 
thinking something along the lines of:

When a module is imported its import search path includes everything it 
does now PLUS the directory that is the top of the module's enclosing 
package structure. That way you wouldn't have to add entries to 
PYTHONPATH for packages that only import from themselves (and stdlib 
and site-packages). Possible? Sensible? What do you think?

Regards,

Rich





More information about the Python-list mailing list