Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is this right? Multiple imports of same module.

Reply
Thread Tools

Is this right? Multiple imports of same module.

 
 
Lou Pecora
Guest
Posts: n/a
 
      03-09-2007
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.
 
Reply With Quote
 
 
 
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      03-09-2007
In <(E-Mail Removed)>, 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
 
Reply With Quote
 
 
 
 
Simon Brunning
Guest
Posts: n/a
 
      03-09-2007
On 3/9/07, Lou Pecora <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed)
http://www.brunningonline.net/simon/blog/
 
Reply With Quote
 
Bart Ogryczak
Guest
Posts: n/a
 
      03-09-2007
On Mar 9, 3:30 pm, Lou Pecora <(E-Mail Removed)> 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.



 
Reply With Quote
 
Lou Pecora
Guest
Posts: n/a
 
      03-09-2007
In article <(E-Mail Removed)>,
Marc 'BlackJack' Rintsch <(E-Mail Removed)> wrote:

> In <(E-Mail Removed)>, 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.
 
Reply With Quote
 
Lou Pecora
Guest
Posts: n/a
 
      03-09-2007
In article <(E-Mail Removed). com>,
"Bart Ogryczak" <(E-Mail Removed)> wrote:

> On Mar 9, 3:30 pm, Lou Pecora <(E-Mail Removed)> 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.
 
Reply With Quote
 
Simon Brunning
Guest
Posts: n/a
 
      03-09-2007
On 3/9/07, Lou Pecora <(E-Mail Removed)> 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
(E-Mail Removed)
http://www.brunningonline.net/simon/blog/
 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      03-09-2007
On Mar 9, 5:18 pm, Lou Pecora <(E-Mail Removed)> 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

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      03-09-2007
On Fri, 09 Mar 2007 11:18:47 -0500, Lou Pecora
<(E-Mail Removed)> 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
(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
Ben Finney
Guest
Posts: n/a
 
      03-10-2007
Lou Pecora <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python3: imports don't see files from same directory? dmitrey Python 3 05-08-2011 06:24 AM
Dynamic imports + relative imports in Python 3 zildjohn01 Python 0 02-22-2011 05:24 PM
Imports System.Data or Imports System.Data.SqlClient? Albert ASP .Net 4 07-10-2008 09:00 AM
Need same viewstate for same page across multiple servers =?Utf-8?B?UHVuaXNoZXI=?= ASP .Net 1 03-23-2006 03:54 PM
I need multiple imports timasmith@hotmail.com Java 42 12-12-2005 07:57 PM



Advertisments