468,511 Members | 1,652 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,511 developers. It's quick & easy.

import keyword behaviour - performance impact if used multipletimes?

Hi,
I've been looking around on Google for the answer to this question, and
it's beginning to really bug me. I'm making some design decisions for
some code I'm writing, and I'm wondering whether (Good Design Decisions
apart), there's a performance impact in importing the same module in two
different files. For example, with the following:

fileA.py
-----------
import psycopg
class A:
....

fileB.py
import psycopg
class B:
a = A()
....

If I run fileB, will this import the psycopg twice, or once only? Is
this something to do with system modules being singletons?

If someone could help me out with this it would be much appreciated.

Regards,
Andrew

--
Andrew James <dr**@gremlinhosting.com>

Jul 18 '05 #1
4 2594
> If I run fileB, will this import the psycopg twice, or once only? Is
this something to do with system modules being singletons?


It will be imported only once. Of course the statement will be read twice -
but as this only happens while reading the *.py the first time, you needn't
bother about performance issues.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Andrew James wrote:
Hi,
I've been looking around on Google for the answer to this question, and
it's beginning to really bug me. I'm making some design decisions for
some code I'm writing, and I'm wondering whether (Good Design Decisions
apart), there's a performance impact in importing the same module in two
different files. For example, with the following:

fileA.py
-----------
import psycopg
class A:
....

fileB.py
import psycopg
class B:
a = A()
....

If I run fileB, will this import the psycopg twice, or once only?
I'm guessing fileB.py should read:
import psycopg
import A
...etc

Anyway, when Python imports modules it stores a reference to them in
sys.modules. Any attempts to import the module again are satisfied using the
cached version already stored in sys.modules.

So, in your example, psycopg is imported once only, and both fileA and fileB
would be referring to the same version of psycopg.
Is
this something to do with system modules being singletons?


They aren't singletons in the GoF design pattern sense. However, Python's import
machinery operates in such a way that it takes effort to get multiple version of
the same module into memory at the same time (it *can* be done, but you have to
work at it).

Cheers,
Nick.
Jul 18 '05 #3
Nick Coghlan wrote:
> Is
> this something to do with system modules being singletons?
They aren't singletons in the GoF design pattern sense. However,

Python's import machinery operates in such a way that it takes effort to get multiple version of the same module into memory at the same time (it *can* be done, but you have to work at it).

Given that this is exactly what I want, how can you do it?

Jul 18 '05 #4
neophyte wrote:
Nick Coghlan wrote:
> Is
> this something to do with system modules being singletons?


They aren't singletons in the GoF design pattern sense. However,


Python's import
machinery operates in such a way that it takes effort to get multiple


version of
the same module into memory at the same time (it *can* be done, but


you have to
work at it).


Given that this is exactly what I want, how can you do it?


If you just want to reload an existing module, use the builtin "reload" function.

Getting multiple versions of a module into sys.modules at the same time isn't
something I've ever actually wanted to do, but the following will do it:

Py> import sys
Py> sys.modules["sys1"] = sys
Py> del sys.modules["sys"]
Py> import sys
Py> import sys1
Py> sys is sys1
False

However:
1. Doing this at all is probably a bad idea (since you may end up duplicating
objects that are meant to be unique within the process, and any C-extension code
will still be shared between the multiple versions of the module)
2. Doing it to 'sys' like I just did is an even worse idea, since you
*definitely* end up doing 1 :)

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by sandy | last post: by
5 posts views Thread by Ben Long | last post: by
16 posts views Thread by ooze | last post: by
14 posts views Thread by Zeng | last post: by
8 posts views Thread by John S | last post: by
2 posts views Thread by 1944USA | last post: by
7 posts views Thread by Magnus | last post: by
7 posts views Thread by Ron Adam | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.