473,386 Members | 2,042 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,386 software developers and data experts.

Importing again and again

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

Jun 8 '06 #1
9 1347

abcd wrote:
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)

Jun 8 '06 #2
abcd wrote:
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:
import foo I'm being imported!
stuff
stuff
stuff
stuff

Jun 8 '06 #3
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

Jun 8 '06 #4
"abcd" <co*******@gmail.com> wrote:
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?

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jun 8 '06 #5

"John Bokma" <jo**@castleamber.com> wrote in message
news:Xn*************************@130.133.1.4...
def foo():
import bar
bar.printStuff()

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

Jun 8 '06 #6
"Terry Reedy" <tj*****@udel.edu> wrote:

"John Bokma" <jo**@castleamber.com> wrote in message
news:Xn*************************@130.133.1.4...
def foo():
import bar
bar.printStuff()

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


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

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jun 8 '06 #7
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
Jun 9 '06 #8
abcd wrote:
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jun 9 '06 #9
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
Jun 9 '06 #10

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

Similar topics

4
by: jean-marc | last post by:
As an application programmer, I'm not well versed in the material aspects of computing (memory, cpu, bus and all). My understanding of imports in Python is such: the __main__ program is the center...
3
by: Mark | last post by:
All, As you can see below, I have had problems with inquisitive souls looking at data they shouldn't be. I though disabling the ability to create new databases using my workgroup would stop this...
1
by: DS | last post by:
Anyone know about importing text from notepad, using a Pocket PC into Access on a Desktop then back again? Thaks DS
9
by: jillandgordon | last post by:
I am trying to import an excel file into Access 97. It looks perfectly all right but, every time I try to import it, I get to the lst step and am told that it was not imported due to an error. ...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
2
by: Snozz | last post by:
The short of it: If you needed to import a CSV file of a certain structure on a regular basis(say 32 csv files, each to one a table in 32 databases), what would be your first instinct on how to...
11
by: panic attack | last post by:
Hello everbody, Our system is using Sql Server 2000 on Windows XP / Windows 2000 We have a text file needs to be imported into Sql Server 2000 as a table. But we are facing a problem which is,...
5
by: hharriel | last post by:
Hi, I am hoping someone can help me with an issue I am having with excel and ms access. I have collected data (which are in individual excel files) from 49 different school districts. All...
5
imrosie
by: imrosie | last post by:
I need to import existing customer & order records into a new db. I need help with the best method of doing this. I tried, I've read forums, tutorials.....what I'm doing isn't working. I've tried...
7
by: Hussein B | last post by:
Hey, Suppose I have a Python application consists of many modules (lets say it is a Django application). If all the modules files are importing sys module, how many times the sys module will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.