Is this right? Multiple imports of same module.

L

Lou Pecora

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.
 
M

Marc 'BlackJack' Rintsch

*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
 
S

Simon Brunning

*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.
 
B

Bart Ogryczak

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.
 
L

Lou Pecora

Marc 'BlackJack' Rintsch said:
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.
 
L

Lou Pecora

"Bart Ogryczak said:
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.
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.
 
M

Michele Simionato

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
 
D

Dennis Lee Bieber

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 address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
B

Ben Finney

Lou Pecora said:
['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.
 
L

Lou Pecora

Dennis Lee Bieber said:
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.
 
L

Lou Pecora

"Simon Brunning said:
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.
 
L

Lou Pecora

Ben Finney said:
Lou Pecora said:
['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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top