Module executed twice when imported!

Discussion in 'Python' started by Michael Abbott, Jun 28, 2006.

  1. It seems to be an invariant of Python (insofar as Python has invariants)
    that a module is executed at most once in a Python session. I have a
    rather bizzare example that breaks this invariant: can anyone enlighten
    me as to what is going on?

    --- test.py ---
    import imptest
    execfile('subtest.py', dict(__name__ = 'subtest.py'))
    --- imptest.py ---
    print 'Imptest imported'
    --- subtest.py ---
    import imptest
    ---

    $ python test.py
    Imptest imported
    Imptest imported
    $

    Hmm. If the value __name__ is omitted from the dictionary, or if its
    value doesn't match at least 'subtest.' then the message is printed only
    once (for example, passing dict(__name__='subtest') produces one
    "imported" message).

    This happens with both python 2.4 and 2.3. I don't get it.
    Michael Abbott, Jun 28, 2006
    #1
    1. Advertising

  2. Michael Abbott

    Simon Forman Guest

    Michael Abbott wrote:
    > It seems to be an invariant of Python (insofar as Python has invariants)
    > that a module is executed at most once in a Python session. I have a
    > rather bizzare example that breaks this invariant: can anyone enlighten
    > me as to what is going on?
    >
    > --- test.py ---
    > import imptest
    > execfile('subtest.py', dict(__name__ = 'subtest.py'))
    > --- imptest.py ---
    > print 'Imptest imported'
    > --- subtest.py ---
    > import imptest
    > ---
    >
    > $ python test.py
    > Imptest imported
    > Imptest imported
    > $
    >
    > Hmm. If the value __name__ is omitted from the dictionary, or if its
    > value doesn't match at least 'subtest.' then the message is printed only
    > once (for example, passing dict(__name__='subtest') produces one
    > "imported" message).
    >
    > This happens with both python 2.4 and 2.3. I don't get it.


    I don't get it either but there may be a clue in the docs for the
    execfile() function
    (http://docs.python.org/lib/built-in-funcs.html#l2h-24)

    "It is different from the import statement in that it does not use the
    module administration --"

    I don't know why the globals dict you're passing in would change the
    behavior though. Hopefully someone else can enlighten us both.

    Peace,
    ~Simon
    Simon Forman, Jun 28, 2006
    #2
    1. Advertising

  3. Michael Abbott a écrit :
    > It seems to be an invariant of Python (insofar as Python has invariants)
    > that a module is executed at most once in a Python session. I have a
    > rather bizzare example that breaks this invariant: can anyone enlighten
    > me as to what is going on?
    >
    > --- test.py ---
    > import imptest
    > execfile('subtest.py', dict(__name__ = 'subtest.py'))
    > --- imptest.py ---
    > print 'Imptest imported'
    > --- subtest.py ---
    > import imptest
    > ---
    >
    > $ python test.py
    > Imptest imported
    > Imptest imported
    > $
    >
    > Hmm. If the value __name__ is omitted from the dictionary, or if its
    > value doesn't match at least 'subtest.' then the message is printed only
    > once (for example, passing dict(__name__='subtest') produces one
    > "imported" message).


    During module evaluation, __name__ is usually set to the name of the
    module *object*, not the name of the *file*. I suspect that it has
    something to do with your observation.
    Bruno Desthuilliers, Jun 29, 2006
    #3
  4. Re: Import bug: Module executed twice when imported!

    Bump

    In article <>,
    Michael Abbott <> wrote:

    > --- test.py ---
    > import imptest
    > execfile('subtest.py', dict(__name__ = 'subtest.py'))
    > --- imptest.py ---
    > print 'Imptest imported'
    > --- subtest.py ---
    > import imptest
    > ---
    >
    > $ python test.py
    > Imptest imported
    > Imptest imported
    > $


    I claim this as an unreported (and highly obscure) Python bug.
    Michael Abbott, Jun 30, 2006
    #4
  5. Michael Abbott

    John Salerno Guest

    Simon Forman wrote:

    > I don't get it either but there may be a clue in the docs for the
    > execfile() function
    > (http://docs.python.org/lib/built-in-funcs.html#l2h-24)
    >
    > "It is different from the import statement in that it does not use the
    > module administration --"


    Just after the above statement, it also says:

    "it reads the file unconditionally and does not create a new module."

    so perhaps that means that it isn't really being imported the second
    time, just that the contents are being executed?
    John Salerno, Jun 30, 2006
    #5
  6. In article <05epg.2385$>,
    John Salerno <> wrote:

    > > (http://docs.python.org/lib/built-in-funcs.html#l2h-24)
    > > "It is different from the import statement in that it does not use the
    > > module administration --"

    >
    > Just after the above statement, it also says:
    >
    > "it reads the file unconditionally and does not create a new module."
    >
    > so perhaps that means that it isn't really being imported the second
    > time, just that the contents are being executed?


    Interesting thought.

    I'm not convinced, though: firstly, I read that statement as describing
    what happens to the file named in the execfile() statement; and
    secondly, the problem *only* happens if the global dictionary passed to
    execfile() has a '__name__' and if the value of that key is sufficiently
    close to the name of the file being passed to execfile().

    I found that passing __name__='whatever' resulted in normal import
    behaviour, and so does __name__='subtest', but curiously enough passing
    __name__='subtest.' results in the double import.
    Michael Abbott, Jun 30, 2006
    #6
  7. Michael Abbott

    Peter Maas Guest

    Re: Import bug: Module executed twice when imported!

    Michael Abbott wrote:
    > In article <>,
    > Michael Abbott <> wrote:
    >
    >> --- test.py ---
    >> import imptest
    >> execfile('subtest.py', dict(__name__ = 'subtest.py'))
    >> --- imptest.py ---
    >> print 'Imptest imported'
    >> --- subtest.py ---
    >> import imptest
    >> ---
    >>
    >> $ python test.py
    >> Imptest imported
    >> Imptest imported
    >> $

    >
    > I claim this as an unreported (and highly obscure) Python bug.


    The docs tell us
    (http://www.python.org/doc/2.4.2/lib/built-in-funcs.html):

    ------------------------- begin -------------------------------
    execfile(filename[, globals[, locals]])

    This function is similar to the exec statement, but parses a file
    instead of a string. It is different from the import statement in that
    it does not use the module administration -- it reads the file
    unconditionally and does not create a new module.
    ------------------------- end ---------------------------------

    I claim this as a well documented (and thus exspectable) Python behaviour.
    execfile() just executes a file unconditionally without searching in
    sys.modules. That's its purpose, otherwise it would be a synonym of
    the import statement.

    Peter Maas, Aachen
    Peter Maas, Jun 30, 2006
    #7
  8. Michael Abbott

    Georg Brandl Guest

    Michael Abbott wrote:
    > In article <05epg.2385$>,
    > John Salerno <> wrote:
    >
    >> > (http://docs.python.org/lib/built-in-funcs.html#l2h-24)
    >> > "It is different from the import statement in that it does not use the
    >> > module administration --"

    >>
    >> Just after the above statement, it also says:
    >>
    >> "it reads the file unconditionally and does not create a new module."
    >>
    >> so perhaps that means that it isn't really being imported the second
    >> time, just that the contents are being executed?

    >
    > Interesting thought.
    >
    > I'm not convinced, though: firstly, I read that statement as describing
    > what happens to the file named in the execfile() statement; and
    > secondly, the problem *only* happens if the global dictionary passed to
    > execfile() has a '__name__' and if the value of that key is sufficiently
    > close to the name of the file being passed to execfile().
    >
    > I found that passing __name__='whatever' resulted in normal import
    > behaviour, and so does __name__='subtest', but curiously enough passing
    > __name__='subtest.' results in the double import.


    That's because __name__ is normally set to the module's name in the package
    hierarchy. When you set it to "some1.some2", Python thinks it's
    in a subpackage and when it does "import imptest" Python searches for
    the module "some1.imptest", not for "imptest", and finds it in the
    current directory (which it assumes to be the package directory).

    You can verify this by inspecting sys.modules after the execfile() call.

    So it's not a bug, but expected behavior.

    Georg
    Georg Brandl, Jun 30, 2006
    #8
  9. Michael Abbott

    Georg Brandl Guest

    Re: Import bug: Module executed twice when imported!

    Peter Maas wrote:

    > The docs tell us
    > (http://www.python.org/doc/2.4.2/lib/built-in-funcs.html):
    >
    > ------------------------- begin -------------------------------
    > execfile(filename[, globals[, locals]])
    >
    > This function is similar to the exec statement, but parses a file
    > instead of a string. It is different from the import statement in that
    > it does not use the module administration -- it reads the file
    > unconditionally and does not create a new module.
    > ------------------------- end ---------------------------------
    >
    > I claim this as a well documented (and thus exspectable) Python behaviour.
    > execfile() just executes a file unconditionally without searching in
    > sys.modules. That's its purpose, otherwise it would be a synonym of
    > the import statement.


    This has nothing directly to do with the "problem" since the seemingly
    double imported module is not the one that's execfile'd.

    Georg
    Georg Brandl, Jun 30, 2006
    #9
  10. In article <e844h9$qkt$>,
    Georg Brandl <> wrote:
    > That's because __name__ is normally set to the module's name in the package
    > hierarchy. When you set it to "some1.some2", Python thinks it's
    > in a subpackage


    Ahhhh.

    So what I *should* have set it to is the module name *without*
    extension, i.e. __name__='imptest'

    Thank you. Now I think I understand it.
    Michael Abbott, Jun 30, 2006
    #10
  11. Michael Abbott

    Georg Brandl Guest

    Michael Abbott wrote:
    > In article <e844h9$qkt$>,
    > Georg Brandl <> wrote:
    >> That's because __name__ is normally set to the module's name in the package
    >> hierarchy. When you set it to "some1.some2", Python thinks it's
    >> in a subpackage

    >
    > Ahhhh.
    >
    > So what I *should* have set it to is the module name *without*
    > extension, i.e. __name__='imptest'
    >
    > Thank you. Now I think I understand it.


    You're welcome. This is indeed not easy to look through.

    Georg
    Georg Brandl, Jul 1, 2006
    #11
    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. Stephan Schulz

    Imported or executed?

    Stephan Schulz, Feb 16, 2005, in forum: Python
    Replies:
    3
    Views:
    357
    Stephan Schulz
    Feb 16, 2005
  2. Stef Mientki
    Replies:
    1
    Views:
    311
    ryles
    Oct 11, 2009
  3. Dave Angel
    Replies:
    2
    Views:
    364
    Dave Angel
    Oct 12, 2009
  4. Dun Peal
    Replies:
    10
    Views:
    459
    Chris Rebert
    May 3, 2011
  5. Volker Nicolai
    Replies:
    9
    Views:
    951
    Fabian Pilkowski
    Jul 4, 2005
Loading...

Share This Page