Absolute Imports and PYTHONPATH

Discussion in 'Python' started by Richard Cooper, May 5, 2004.

  1. 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
    Richard Cooper, May 5, 2004
    #1
    1. Advertising

  2. Richard Cooper

    John Roth Guest

    "Richard Cooper" <> wrote in message
    news:...
    > 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.
    >
    > Regards,
    >
    > Rich
    >
    >
    John Roth, May 6, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    313
  2. Albert
    Replies:
    4
    Views:
    10,820
    Albert
    Jul 10, 2008
  3. Steven D'Aprano

    Absolute imports in Python 2.4

    Steven D'Aprano, Jun 2, 2009, in forum: Python
    Replies:
    2
    Views:
    895
    Steven D'Aprano
    Jun 4, 2009
  4. zildjohn01
    Replies:
    0
    Views:
    626
    zildjohn01
    Feb 22, 2011
  5. Victor Hooi
    Replies:
    1
    Views:
    96
    Devin Jeanpierre
    Nov 25, 2013
Loading...

Share This Page