Is this right? Multiple imports of same module.

Discussion in 'Python' started by Lou Pecora, Mar 9, 2007.

  1. Lou Pecora

    Lou Pecora Guest

    From what I gleaned on some messages this is the way multiple imports of
    the same module work. Say, I have 3 modules (mod1.py, mod2.py, and
    mod3.py) and in the same session they all import another module
    mymodule.py like this:

    *In mod1.py

    import mymodule

    *In mod2.py

    import mymodule

    *In mod3.py

    import mymodule as MM

    Then mymodule is imported only once, but each module has access to it
    through the module name (mod1 and mod2) and the alias MM (mod3). Is
    that right?

    I was concerned about multiple imports and efficiency.

    Thanks for any info.

    -- Lou Pecora (my views are my own) REMOVE THIS to email me.
     
    Lou Pecora, Mar 9, 2007
    #1
    1. Advertising

  2. In <>, Lou Pecora wrote:

    > *In mod1.py
    >
    > import mymodule
    >
    > *In mod2.py
    >
    > import mymodule
    >
    > *In mod3.py
    >
    > import mymodule as MM
    >
    > Then mymodule is imported only once, but each module has access to it
    > through the module name (mod1 and mod2) and the alias MM (mod3). Is
    > that right?


    Yes, that's correct.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Mar 9, 2007
    #2
    1. Advertising

  3. On 3/9/07, Lou Pecora <> wrote:
    >
    > *In mod1.py
    >
    > import mymodule
    >
    > *In mod2.py
    >
    > import mymodule
    >
    > *In mod3.py
    >
    > import mymodule as MM
    >
    > Then mymodule is imported only once, but each module has access to it
    > through the module name (mod1 and mod2) and the alias MM (mod3). Is
    > that right?


    Yup, and occasionally it's useful to do stuff like this...

    > I was concerned about multiple imports and efficiency.


    .... but not for this reason. Subsequent imports of an already loaded
    module are very quick.

    --
    Cheers,
    Simon B

    http://www.brunningonline.net/simon/blog/
     
    Simon Brunning, Mar 9, 2007
    #3
  4. On Mar 9, 3:30 pm, Lou Pecora <> wrote:
    > Then mymodule is imported only once, but each module has access to it
    > through the module name (mod1 and mod2) and the alias MM (mod3). Is
    > that right?


    Yes, it is.

    > I was concerned about multiple imports and efficiency.


    If the module is already loaded, it won't be loaded again. Dictionary
    of loaded modules is avaliable as sys.modules.
     
    Bart Ogryczak, Mar 9, 2007
    #4
  5. Lou Pecora

    Lou Pecora Guest

    In article <>,
    Marc 'BlackJack' Rintsch <> wrote:

    > In <>, Lou Pecora wrote:
    >
    > > *In mod1.py
    > >
    > > import mymodule
    > >
    > > *In mod2.py
    > >
    > > import mymodule
    > >
    > > *In mod3.py
    > >
    > > import mymodule as MM
    > >
    > > Then mymodule is imported only once, but each module has access to it
    > > through the module name (mod1 and mod2) and the alias MM (mod3). Is
    > > that right?

    >
    > Yes, that's correct.
    >
    > Ciao,
    > Marc 'BlackJack' Rintsch


    Thank you.

    A further confusion (if I may):

    I have noticed that using from xxx import * can lead to problems when
    trying to access variables in the xxx module.

    E.g.

    -- File f2.py

    imvar=1

    def prn():
    print imvar

    -- File junk.py

    from f2 import *
    prn()
    imvar=2
    prn()

    -- Running junk.py gives the output:

    1
    1

    Not what I would expect which is,

    1
    2

    Namespaces get confusing. I have begun to switch to using 'import f2
    as' which gives the 2nd expected result. Safer, but still a bit puzzling.

    -- Lou Pecora (my views are my own) REMOVE THIS to email me.
     
    Lou Pecora, Mar 9, 2007
    #5
  6. Lou Pecora

    Lou Pecora Guest

    In article <>,
    "Bart Ogryczak" <> wrote:

    > On Mar 9, 3:30 pm, Lou Pecora <> wrote:
    > > Then mymodule is imported only once, but each module has access to it
    > > through the module name (mod1 and mod2) and the alias MM (mod3). Is
    > > that right?

    >
    > Yes, it is.


    Praise the Lord. :) I got one right in the namespaces puzzles. (But
    see my followup on using 'from XXX import *' in this thread. I'm still
    not a namespace guru.

    > > I was concerned about multiple imports and efficiency.

    >
    > If the module is already loaded, it won't be loaded again. Dictionary
    > of loaded modules is avaliable as sys.modules.


    Good to know. Thank you.

    -- Lou Pecora (my views are my own) REMOVE THIS to email me.
     
    Lou Pecora, Mar 9, 2007
    #6
  7. On 3/9/07, Lou Pecora <> wrote:
    > I have noticed that using from xxx import * can lead to problems when
    > trying to access variables in the xxx module.


    Don't do it, then. ;-)

    --
    Cheers,
    Simon B

    http://www.brunningonline.net/simon/blog/
     
    Simon Brunning, Mar 9, 2007
    #7
  8. On Mar 9, 5:18 pm, Lou Pecora <> wrote:
    > I have noticed that using from xxx import * can lead to problems when
    > trying to access variables in the xxx module.


    ``from xxx import *`` is intended to be used in the interactive
    interpreter
    only.

    Michele Simionato
     
    Michele Simionato, Mar 9, 2007
    #8
  9. On Fri, 09 Mar 2007 11:18:47 -0500, Lou Pecora
    <> declaimed the following in comp.lang.python:


    > A further confusion (if I may):
    >
    > I have noticed that using from xxx import * can lead to problems when
    > trying to access variables in the xxx module.
    >


    For the most part, the recommendation is to /never/ user "from <>
    import *" (the exceptions tend to be things like predefined constants
    for GUI libraries, which often have names that make them unique, and as
    constants one isn't supposed to be changing them).

    > E.g.
    >
    > -- File f2.py
    >
    > imvar=1
    >
    > def prn():
    > print imvar
    >
    > -- File junk.py
    >
    > from f2 import *
    > prn()
    > imvar=2
    > prn()
    >
    > -- Running junk.py gives the output:
    >
    > 1
    > 1
    >
    > Not what I would expect which is,
    >
    > 1
    > 2
    >
    > Namespaces get confusing. I have begun to switch to using 'import f2
    > as' which gives the 2nd expected result. Safer, but still a bit puzzling.
    >


    Expected behavior if you 1) understand how Python variable binding
    works, and 2) that "from <> import *" is equivalent to

    import <>
    name1 = <>.name1
    name2 = <>.name2

    IOWs, one is creating local names that are bound to the objects
    inside the imported module. If, subsequently, one does

    name2 = 123

    one has UNbound the connection to <>.name2 and rebound the local name to
    the integer object 123. Whereas

    <>.name2 = 123

    says "go into the module <>, and rebind its 'name2' to the integer
    object 123"; since you "went inside" to do the rebinding, any other code
    that also uses the <>.name2 reference will see the change inside <>.

    A bare name "assignment" always causes a rebinding of that name. A
    qualified assignment (module, list, dictionary, class instance) "goes
    inside" the top level item to rebind the item inside it:

    module.object = xxx #rebinds name "object" inside module
    list[element] = xxx #rebinds the indexed element inside the list
    dict[key] = xxx #rebinds the indexed (by key) element
    instance.attr = xxx #rebinds the attribute inside the instance
    --
    Wulfraed Dennis Lee Bieber KD6MOG

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Mar 9, 2007
    #9
  10. Lou Pecora

    Ben Finney Guest

    Lou Pecora <> writes:

    > ['import mymodule' in three separate modules]
    >
    > Then mymodule is imported only once, but each module has access to
    > it through the module name (mod1 and mod2) and the alias MM (mod3).
    > Is that right?


    Not quite. The module is imported three times, and is assigned to
    three different namespaces; but the code in the module is executed
    only once, the first time it is imported. Each subsequent import skips
    the "execute the code" step.

    --
    \ "My classmates would copulate with anything that moved, but I |
    `\ never saw any reason to limit myself." -- Emo Philips |
    _o__) |
    Ben Finney
     
    Ben Finney, Mar 10, 2007
    #10
  11. Lou Pecora

    Lou Pecora Guest

    In article <DThIh.10891$>,
    Dennis Lee Bieber <> wrote:

    >
    > Expected behavior if you 1) understand how Python variable binding
    > works, and 2) that "from <> import *" is equivalent to
    >
    > import <>
    > name1 = <>.name1
    > name2 = <>.name2
    >
    > IOWs, one is creating local names that are bound to the objects
    > inside the imported module. If, subsequently, one does
    >
    > name2 = 123
    >
    > one has UNbound the connection to <>.name2 and rebound the local name to
    > the integer object 123. Whereas
    >
    > <>.name2 = 123
    >
    > says "go into the module <>, and rebind its 'name2' to the integer
    > object 123"; since you "went inside" to do the rebinding, any other code
    > that also uses the <>.name2 reference will see the change inside <>.



    Ah! (light bulb goes on) Yes, I get it. Thanks. No more from **
    import *.

    -- Lou Pecora (my views are my own) REMOVE THIS to email me.
     
    Lou Pecora, Mar 10, 2007
    #11
  12. Lou Pecora

    Lou Pecora Guest

    In article <>,
    "Simon Brunning" <> wrote:

    > On 3/9/07, Lou Pecora <> wrote:
    > > I have noticed that using from xxx import * can lead to problems when
    > > trying to access variables in the xxx module.

    >
    > Don't do it, then. ;-)


    I don't anymore. But I find it's good to understand these things since
    there's something fundamental that I am missing that may later be
    useful. This turned out to be the case as someone else in this thread
    (Dennis) gave a very good explanation of the source of the problem.

    -- Lou Pecora (my views are my own) REMOVE THIS to email me.
     
    Lou Pecora, Mar 10, 2007
    #12
  13. Lou Pecora

    Lou Pecora Guest

    In article <>,
    Ben Finney <> wrote:

    > Lou Pecora <> writes:
    >
    > > ['import mymodule' in three separate modules]
    > >
    > > Then mymodule is imported only once, but each module has access to
    > > it through the module name (mod1 and mod2) and the alias MM (mod3).
    > > Is that right?

    >
    > Not quite. The module is imported three times, and is assigned to
    > three different namespaces; but the code in the module is executed
    > only once, the first time it is imported. Each subsequent import skips
    > the "execute the code" step.


    Thanks. That's clear.

    -- Lou Pecora (my views are my own) REMOVE THIS to email me.
     
    Lou Pecora, Mar 10, 2007
    #13
    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. Adrian Holovaty
    Replies:
    0
    Views:
    274
    Adrian Holovaty
    Nov 6, 2004
  2. Albert
    Replies:
    4
    Views:
    10,889
    Albert
    Jul 10, 2008
  3. zildjohn01
    Replies:
    0
    Views:
    653
    zildjohn01
    Feb 22, 2011
  4. dmitrey
    Replies:
    3
    Views:
    1,961
    Benjamin Kaplan
    May 8, 2011
  5. Victor Hooi
    Replies:
    1
    Views:
    108
    Devin Jeanpierre
    Nov 25, 2013
Loading...

Share This Page