467,894 Members | 1,461 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Consequences of importing the same module multiple times in C++?

Hi,

I'm currently using boost::python::import() to import Python modules,
so I'm not sure exactly which Python API function it is calling to
import these files. I posted to the Boost.Python mailing list with
this question and they said I'd probably get a better answer here, so
here it goes...

If I do the following:

using namespace boost::python;
import( "__main__" ).attr( "new_global" ) = 40.0f;
import( "__main__" ).attr( "another_global" ) = 100.0f:

Notice that I'm importing twice. What would be the performance
consequences of this? Do both import operations query the disk for the
module and load it into memory? Will the second call simply reference
a cached version of the module loaded at the first import() call?

Thanks.
Oct 24 '08 #1
  • viewed: 1945
Share:
2 Replies
On Fri, 24 Oct 2008 12:23:18 -0700, Robert Dailey wrote:
Hi,

I'm currently using boost::python::import() to import Python modules, so
I'm not sure exactly which Python API function it is calling to import
these files. I posted to the Boost.Python mailing list with this
question and they said I'd probably get a better answer here, so here it
goes...

If I do the following:

using namespace boost::python;
import( "__main__" ).attr( "new_global" ) = 40.0f; import( "__main__"
).attr( "another_global" ) = 100.0f:

Notice that I'm importing twice. What would be the performance
consequences of this? Do both import operations query the disk for the
module and load it into memory? Will the second call simply reference a
cached version of the module loaded at the first import() call?

Thanks.
I think it does not reload the module. Running python with verbose mode:

blah@blah-laptop:~$ python -v
(snip)
>>import xml
import xml # directory /usr/local/lib/python2.6/xml
# /usr/local/lib/python2.6/xml/__init__.pyc matches /usr/local/lib/
python2.6/xml/__init__.py
import xml # precompiled from /usr/local/lib/python2.6/xml/__init__.pyc
>>import xml
It's also mentioned in the docs: (paraphrased to clarify the points)
'''
The system maintains a table of modules that have been ...
initialized.... When a module name is found..., step (1) is finished. If
not, a search for a module ... . When ... found, it is loaded.
'''
http://www.python.org/doc/2.5.2/ref/import.html

Oct 25 '08 #2
On Oct 24, 2:23*pm, Robert Dailey <rcdai...@gmail.comwrote:
Hi,

I'm currently using boost::python::import() to import Python modules,
so I'm not sure exactly which Python API function it is calling to
import these files. I posted to the Boost.Python mailing list with
this question and they said I'd probably get a better answer here, so
here it goes...

If I do the following:

using namespace boost::python;
import( "__main__" ).attr( "new_global" ) = 40.0f;
import( "__main__" ).attr( "another_global" ) = 100.0f:

Notice that I'm importing twice. What would be the performance
consequences of this? Do both import operations query the disk for the
module and load it into memory? Will the second call simply reference
a cached version of the module loaded at the first import() call?

Thanks.
Docs:

Note
For efficiency reasons, each module is only imported once per
interpreter session. Therefore, if you change your modules, you must
restart the interpreter or, if its just one module you want to test
interactively, use reload(), e.g. reload(modulename).

Oct 25 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by OKB (not okblacke) | last post: by
4 posts views Thread by Active8 | last post: by
12 posts views Thread by qwweeeit | last post: by
14 posts views Thread by T. Crane | last post: by
9 posts views Thread by rbygscrsepda | last post: by
4 posts views Thread by rkmr.em | last post: by
7 posts views Thread by Hussein B | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.