[Numpy-discussion] importing multiarraymodule.c in Python embedding

Sebastien Bardeau bardeau at iram.fr
Wed Feb 7 07:51:31 EST 2007


Hi,

it seems that I have the same trouble, but using Python under Linux:

> I have run into an interesting issue with multiarraymodule.c with 
> regards to embedding Python in a C application on Windows XP. In the 
> application, the following abbreviated sequence was executed
>
>  
>
> 1) Py_Initialize
>
> 2) numpy is imported
>
> 3) Py_Finalize
>
> 4) Py_Initialize
>
> 5) attempt to import numpy results in an error.
>

As an example I wrote a small C program that embeds Python. The steps 
are the same as above:
1) Python is initialized (Py_Initialize)
2) Numpy is "manually" imported (import numpy) under an interactive loop 
(PyRun_InteractiveLoop)
3) Python is finalized (Py_Finalize)
4) Python is initialized again (Py_Initialize)
5) Attempt to import numpy now fails:

 >>> import numpy
Traceback (most recent call last):
  File "<STDIN>", line 1, in <module>
  File "/usr/lib/python2.5/site-packages/numpy/__init__.py", line 36, in 
<module>
    import core
  File "/usr/lib/python2.5/site-packages/numpy/core/__init__.py", line 
5, in <module>
    import multiarray
SystemError: dynamic module not initialized properly

I'm using Numpy 1.0 and Python 2.5 under a Fedora Core 6:
Python 2.5 (r25:51908, Nov 15 2006, 14:24:03)
[GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2

I have not searched further why this happens. I embed Python in a larger 
project and let the choice to the user to start a Python session if he 
needs it. Numpy is automatically imported right after Python 
initialization (because it is needed in my context), so that if user 
starts a session, ends it, and starts a new one, the initialization 
automatically fails at the second time. The solution at the present I 
found is  preventing Python to be finalized (Py_Finalize).

I hope this can help. Thanks.

Sebastien




More information about the NumPy-Discussion mailing list