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

Lie Ryan lie.1296 at
Sun Oct 26 00:24:48 CEST 2008

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 at blah-laptop:~$ python -v
>>> import xml
import xml # directory /usr/local/lib/python2.6/xml
# /usr/local/lib/python2.6/xml/__init__.pyc matches /usr/local/lib/
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.

More information about the Python-list mailing list