Importing again and again

A

abcd

If I have code which imports a module over and over again...say each
time a function is called, does that cause Python to actually re-import
it...or will it skip it once the module has been imported??

for example:

def foo():
import bar
bar.printStuff()

foo()
foo()
foo()
foo()

....will that re-import bar 4 times...or just import it once? is this a
big performance hit?

thanks
 
D

Dustan

abcd said:
If I have code which imports a module over and over again...say each
time a function is called, does that cause Python to actually re-import
it...or will it skip it once the module has been imported??

for example:

def foo():
import bar
bar.printStuff()

foo()
foo()
foo()
foo()

...will that re-import bar 4 times...or just import it once? is this a
big performance hit?

thanks

I don't really know, but I do know that the right way to re-import a
module is:

reload(bar)
 
J

John Salerno

abcd said:
def foo():
import bar
bar.printStuff()

foo()
foo()
foo()
foo()

...will that re-import bar 4 times...or just import it once? is this a
big performance hit?

thanks

Given a file called bar.py with the following contents:

print "I'm being imported!"

def printStuff():
print 'stuff'

I get this output when I import foo.py:
I'm being imported!
stuff
stuff
stuff
stuff
 
L

Laszlo Nagy

abcd írta:
If I have code which imports a module over and over again...say each
time a function is called, does that cause Python to actually re-import
it...or will it skip it once the module has been imported??

for example:

def foo():
import bar
bar.printStuff()

foo()
foo()
foo()
foo()

...will that re-import bar 4 times...or just import it once?
Just once. Try this:

bar.py:

print "I have been imported"

def printStuff():
print "printStuff was called"

foo.py:

def foo():
import bar
bar.printStuff()

foo()
foo()
foo()


The result is:

I have been imported
printStuff was called
printStuff was called
printStuff was called


If you really need to reimport the module, you can do this:

foo.py:

import bar

def foo():
global bar
bar = reload(bar)
bar.printStuff()

foo()
foo()
foo()


The result is:

I have been imported
I have been imported
printStuff was called
I have been imported
printStuff was called
I have been imported
printStuff was called

Is this a big performance hit?
It depends on the size of your 'bar.py' module, and also it depends on
how often you need to change/reload while your program is running.

Best,

Laszlo
 
J

John Bokma

abcd said:
If I have code which imports a module over and over again...say each
time a function is called, does that cause Python to actually re-import
it...or will it skip it once the module has been imported??

for example:

def foo():
import bar
bar.printStuff()

foo()
foo()
foo()
foo()

...will that re-import bar 4 times...or just import it once? is this a
big performance hit?

I am new to Python so this might be a weird question, but it there a
reason why you import bar inside foo?
 
T

Terry Reedy

John Bokma said:
I am new to Python so this might be a weird question, but it there a
reason why you import bar inside foo?

Two possible reasons:
1) delay import until actually needed, if ever.
2) put 'bar' into the function local namespace instead of the module global
namespace

Terry Jan Reedy
 
J

John Bokma

Terry Reedy said:
Two possible reasons:
1) delay import until actually needed, if ever.
2) put 'bar' into the function local namespace instead of the module
global namespace

OK clear and thanks. I was guessing the later, and the first one I
overlooked (Perl works different, you have to do the delay yourself).
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

it's import-ed only once

# magic.py file

#!/usr/bin/python
print "here"
import magic # try to import itself

then try

# bad_magic.py

#!/usr/bin/python
print "here"
import bad_magic
reload(bad_magic)


hth, Daniel
 
S

Steve Holden

abcd said:
If I have code which imports a module over and over again...say each
time a function is called, does that cause Python to actually re-import
it...or will it skip it once the module has been imported??

for example:

def foo():
import bar
bar.printStuff()

foo()
foo()
foo()
foo()

...will that re-import bar 4 times...or just import it once? is this a
big performance hit?

thanks
To address the performance question, by the way, the first thing that
the import process does is look in sys.modules to see whether the module
has already been imported. If so then it uses the module referenced by
sys.modules, so the overhead is pretty small (dicts being quite fast
generally speaking).

So your code is a reasonable way to defer the import of bar until the
function is called, which is why I presumed you coded it that way.

regards
Steve
 
M

Maric Michaud

Le Jeudi 08 Juin 2006 22:02, abcd a écrit :
def foo():
import bar
bar.printStuff()

foo()
foo()
foo()
foo()

...will that re-import bar 4 times...or just import it once? is this a
big performance hit?

Import a module more than once doesn't execute the code of this module more
than once.

I don't know what's your need but as some have spoke of reload I just want to
warn you, reload a module means that you want invalidate the code of this and
replace it by a new one, this is not like a normal but deeper import. Also,
you'll have to deal yourself wiith references to your old code.
Hmmm, the following example should be clear than my explanations :)

n [1]: import temp

In [2]: class a(temp.Base
temp.Base

In [2]: class a(temp.Base
temp.Base

In [2]: class a(temp.Base
temp.Base

In [2]: class a(temp.Base) : pass
...:

In [3]: reload(temp)
Out[3]: <module 'temp' from 'temp.pyc'>

In [4]: class b(temp.Base) : pass
...:

In [7]: b.__base__, a.__base__, b.__base__ is a.__base__
Out[7]: (<class 'temp.Base'>, <class 'temp.Base'>, False)

In [8]: isinstance(a(), temp.Base), isinstance(b(), temp.Base)
Out[8]: (False, True)

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
 

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

Similar Threads


Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top