Absolute Imports and PYTHONPATH

R

Richard Cooper

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
 
J

John Roth

Richard Cooper said:
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?

As far as I'm concerned, the whole notion of Pythonpath is seriously
broken, but I'm not about to suggest replacing it given the discussion
around PEP 328. Redesigning the whole import mechanism is something
that will have to wait for the ever receeding 3.0

What I do is I have a separate command prompt (I'm on Windows)
for each different Pythonpath I need to work with, and it sets the
correct path for the rest of the work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top