Re: code in a module is executed twice (cyclic import problems) ?

Discussion in 'Python' started by Dave Angel, Oct 11, 2009.

  1. Dave Angel

    Dave Angel Guest

    (please don't top-post. Put your reply *after* the message you're quoting.)

    Stef Mientki wrote:
    > <div class="moz-text-flowed" style="font-family: -moz-fixed">thanks
    > very much Stephen,
    >
    > This is the first time I become aware of the difference between script
    > and module !!
    > Starting with the wrong book "Learning Python" second edition, from
    > Lutz and Ascher, based on Python 2.3"
    > in combination with using Python only from a high level IDE
    > (PyScripter), (never seeing the m-switch),
    > I never was aware of this important difference.
    > A quick Googling on "python module vs script" doesn't reveal many
    > (good) links,
    > the best one I found is
    > http://effbot.org/zone/import-confusion.htm
    >
    > thanks again,
    > Stef Mientki
    > <snip>

    The point you should get from that link is

    "Don't do circular imports. Ever."

    It's generally worse if the circle includes the original script, treated
    as a module, but even between modules it can get you into trouble in
    many subtle ways. Some of them cause runtime errors, so you'll "fix"
    them. Some of them will just fail quietly, and you'll be searching for
    subtle bugs. I don't agree with the author's "advice" that sometimes
    moving the import to the end helps. Best advice is to break the circle,
    by "rearranging" the modules, moving commonly needed symbols into
    someplace else that both import.

    It's probably safe if none of the modules in the circle has any
    top-level code that references imported symbols. But top-level code
    includes default values for function definitions, and class initializers
    for class definitions.

    Does anybody know if there's a way to activate a warning for this?

    DaveA
     
    Dave Angel, Oct 11, 2009
    #1
    1. Advertising

  2. Dave Angel

    greg Guest

    Dave Angel wrote:

    > The point you should get from that link is
    >
    > "Don't do circular imports. Ever."


    No, I would say the lesson to be learned from this is
    "don't use the same file as both a main script and an
    imported module".

    I would create another file called e.g. 'main.py' that
    simply contains

    import B

    and use that as the main script.

    Another benefit is that the compiled bytecode of B will
    be saved, so it will start up faster the next time you
    run it. That won't happen if you use B directly as a
    main script.

    (Circular imports are a different issue. There are perfectly
    legitimate uses for them, and they're fine as long as you
    understand the potential pitfalls and take steps to avoid
    them. Although it's probably best to avoid them until you
    do understand all the issues.)

    --
    Greg
     
    greg, Oct 12, 2009
    #2
    1. Advertising

  3. Dave Angel

    Dave Angel Guest

    greg wrote:
    > <div class="moz-text-flowed" style="font-family: -moz-fixed">Dave
    > Angel wrote:
    >
    >> The point you should get from that link is
    >>
    >> "Don't do circular imports. Ever."

    >
    > No, I would say the lesson to be learned from this is
    > "don't use the same file as both a main script and an
    > imported module".
    >
    > I would create another file called e.g. 'main.py' that
    > simply contains
    >
    > import B
    >
    > and use that as the main script.
    >
    > Another benefit is that the compiled bytecode of B will
    > be saved, so it will start up faster the next time you
    > run it. That won't happen if you use B directly as a
    > main script.
    >
    > (Circular imports are a different issue. There are perfectly
    > legitimate uses for them, and they're fine as long as you
    > understand the potential pitfalls and take steps to avoid
    > them. Although it's probably best to avoid them until you
    > do understand all the issues.)
    >

    You prove my point by trying to contradict it. Importing a script from
    the same or another module is a circular import, and therefore should be
    avoided. If you understand all the issues, you can get away with it,
    but few programmers understand them all like they think they do.

    The *only* difference between this circular import and any other is that
    the problems it can trigger are a little more obvious, and a lot more
    likely.

    DaveA
     
    Dave Angel, Oct 12, 2009
    #3
    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. plb
    Replies:
    0
    Views:
    427
  2. plb
    Replies:
    0
    Views:
    346
  3. Michael Abbott

    Module executed twice when imported!

    Michael Abbott, Jun 28, 2006, in forum: Python
    Replies:
    10
    Views:
    500
    Georg Brandl
    Jul 1, 2006
  4. Stef Mientki
    Replies:
    1
    Views:
    331
    ryles
    Oct 11, 2009
  5. Peng Yu
    Replies:
    0
    Views:
    353
    Peng Yu
    Oct 31, 2009
Loading...

Share This Page