473,387 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Is this right? Multiple imports of same module.

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.
Mar 9 '07 #1
11 7657
In <pe**************************@ra.nrl.navy.mil>, 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
Mar 9 '07 #2
On 3/9/07, Lou Pecora <pe****@anvil.nrl.navy.milwrote:
>
*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
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/
Mar 9 '07 #3
On Mar 9, 3:30 pm, Lou Pecora <pec...@anvil.nrl.navy.milwrote:
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.

Mar 9 '07 #4
In article <pa****************************@gmx.net>,
Marc 'BlackJack' Rintsch <bj****@gmx.netwrote:
In <pe**************************@ra.nrl.navy.mil>, 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.
Mar 9 '07 #5
In article <11**********************@64g2000cwx.googlegroups. com>,
"Bart Ogryczak" <B.********@gmail.comwrote:
On Mar 9, 3:30 pm, Lou Pecora <pec...@anvil.nrl.navy.milwrote:
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.
Mar 9 '07 #6
On 3/9/07, Lou Pecora <pe****@anvil.nrl.navy.milwrote:
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
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/
Mar 9 '07 #7
On Mar 9, 5:18 pm, Lou Pecora <pec...@anvil.nrl.navy.milwrote:
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

Mar 9 '07 #8
Lou Pecora <pe****@anvil.nrl.navy.milwrites:
['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

Mar 10 '07 #9
In article <DT******************@newsread1.news.pas.earthlink .net>,
Dennis Lee Bieber <wl*****@ix.netcom.comwrote:
>
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.
Mar 10 '07 #10
In article <ma***************************************@python. org>,
"Simon Brunning" <si***@brunningonline.netwrote:
On 3/9/07, Lou Pecora <pe****@anvil.nrl.navy.milwrote:
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.
Mar 10 '07 #11
In article <ma***************************************@python. org>,
Ben Finney <bi****************@benfinney.id.auwrote:
Lou Pecora <pe****@anvil.nrl.navy.milwrites:
['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.
Mar 10 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: max(01)* | last post by:
hi everybody. suppose that code-1.py imports code-2.py and code-3.py (because it uses names from both), and that code-2.py imports code-3.py. if python were c, code-1.c should only *include*...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
2
by: Matthias Kramm | last post by:
Hi All, I'm having a little bit of trouble using the "imp" module to dynamically import modules. It seems that somehow cyclic references of modules don't work. I'm unable to get the following...
1
by: n4ixt | last post by:
I have two macros I used to use in VS 2003. I recently tried to import them for use in VS 2005, but they don't seem to work. I open the macro explorer, right click and do run, but it's like VS...
1
by: Thomas Wittek | last post by:
Hi! Is there any possibility/tool to automatically organize the imports at the beginning of a module? I don't mean automatic imports like autoimp does as I like seeing where my...
0
by: Christian Bird | last post by:
Is it possible to import multiple modules with the same name from different locations? I'm using two different pieces of software, both of which have a module named util.py. I know that I can...
0
by: Kay Schluehr | last post by:
Since their introduction in Python 2.5 I only reviewed the new "relative import" notation briefly by reading the "What's new in Python 2.5" article. Now I wanted checkout if I get comfortable with...
5
by: Matthew Wilson | last post by:
I started off with a module that defined a class Vehicle, and then subclasses Car and Motorcycle. In the Car class, for some bizarre reason, I instantiated a Motorcycle. Please pretend that...
3
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.