[C++-sig] [Boost.Python] Importing twice causes error in MSVSwith Boost.Python extension?

English, Mark Mark.English at rbccm.com
Mon Aug 20 17:36:31 CEST 2007


> -----Original Message-----
> Behalf Of David Abrahams
> Sent: 20 August 2007 15:33
> Subject: Re: [C++-sig] [Boost.Python] Importing twice causes error in
> MSVSwith Boost.Python extension?
> 
> on Wed Aug 15 2007, Lawrence Spector 
> <Lawrence.Spector-AT-CanfieldSci.com> wrote:
> 
> > In one case I use “PyImport_ImportModule”.  Later on, I do 
> “PyRun_String”.  Either way, same
> > deal.  I import the module and the first time it succeeds.  
> That means the same type converters are being registered twice.
> Python shouldn't let you actually load the same module twice (i.e. run
> its init function) unless you do Py_Finalize(), which isn't supported
> by Boost.Python.
FWIW confusingly it is possible to import the same module file twice using different relative/absolute paths.
This can be avoided by always using absolute import paths.
In a sense though it's not the same module due to the different import paths so six of one == half dozen other.

For example (not actually tried verbatim but should make the idea clear despite potential typos)
assuming there isn't a mod01 or sub01 anywhere on PYTHONPATH:
md sub01
touch sub01/__init__.py
echo print 'Importing mod01' > sub01/mod01.py
python
>>> import sys, os
>>> sys.path.append(os.path.join(os.getcwd(), 'sub01'))
>>> os.chdir('sub01')
>>> import mod01
Importing mod01
>>> import mod01 #<--- Note nothing happens here as expected
>>> import sub01.mod01 
Importing mod01 #<--- Note second import
>>> import sub01.mod01 #<-- Nothing again

This happens at least on Windows 2000 with Python 2.3.6 up to Python 2.5.1,
and I'd expect that at least on all other Windows platforms.

If you're uncertain what's importing what, it's also sometimes helpful
to run python with the -v flag which shows what imports are occurring.

Hope that helps,
Mark
________________________________________

This E-Mail (including any attachments) may contain privileged or confidential information.  It is intended only for the addressee(s) indicated above.

The sender does not waive any of its rights, privileges or other protections respecting this information.  

Any distribution, copying or other use of this E-Mail or the information it contains, by other than an intended recipient, is not sanctioned and is prohibited.

If you received this E-Mail in error, please delete it and advise the sender (by return E-Mail or otherwise) immediately. 

This E-Mail (including any attachments) has been scanned for viruses. 

It is believed to be free of any virus or other defect that might affect any computer system into which it is received and opened. 

However, it is the responsibility of the recipient to ensure that it is virus free. 

The sender accepts no responsibility for any loss or damage arising in any way from its use.

E-Mail received by or sent from RBC Capital Markets is subject to review by Supervisory personnel. 

Such communications are retained and may be produced to regulatory authorities or others with legal rights to the information.

IRS CIRCULAR 230 NOTICE:  TO COMPLY WITH U.S. TREASURY REGULATIONS, WE ADVISE YOU THAT ANY U.S. FEDERAL TAX ADVISE INCLUDED IN THIS COMMUNICATION IS NOT INTENDED OR WRITTEN TO BE USED, AND CANNOT BE USED, TO AVOID ANY U.S. FEDERAL TAX PENALTIES OR TO PROMOTE, MARKET, OR RECOMMEND TO ANOTHER PARTY ANY TRANSACTION OR MATTER.


More information about the Cplusplus-sig mailing list