473,385 Members | 1,869 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,385 software developers and data experts.

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
2 2193
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 it’s just one module you want to test
interactively, use reload(), e.g. reload(modulename).

Oct 25 '08 #3

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

Similar topics

0
by: OKB (not okblacke) | last post by:
I'm fooling around with some MUD server code, and I want to add a "reload" command that will let MUD wizards reload the server modules, so that changes to the MUD parser and such can be effected...
4
by: Active8 | last post by:
I did this once and can't remember how <blush> so I read the reportlab user guid. It says to unzip the reportlab archive - this is on w2k, BTW, with Python23 - to a directory and make a file...
12
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In...
4
by: Brian K. | last post by:
I am trying to split up and secure a database that we've been using on a network for a few years, following the procedure listed in Q304932 from microsoft. I create a new blank database for...
14
by: T. Crane | last post by:
Hi, When troubleshooting code that's saved in a text file, I often find that I want to make a change to it, re-save it, then reimport it. However, just typing import myTestCode doesn't...
9
by: rbygscrsepda | last post by:
Hi, I'm a newbie at Python. :) Right now it's not letting me import * from any relative package name--i.e., a name that starts with a dot. For instance, none of the following work: from . import...
4
by: rkmr.em | last post by:
Hi I have a function data, that I need to import from a file data, in the directory data If I do this from python interactive shell (linux fedora core 8) from dir /home/mark it works fine: ...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.