Reputation: 493
I am failing to open numpy with the Python C API. I have the following code
#include<Python.h>
int main()
{
Py_Initialize();
PyRun_SimpleString("import numpy");
PyObject* numpy = PyImport_ImportModule("numpy");
Py_Finalize();
return 0;
}
The line PyRun_SimpleString("import numpy")
prints to console:
Traceback (most recent call last): File "", line 1, in File "C:\Users\matt.heath\AppData\Local\Continuum\Anaconda3\lib\site-packages\numpy__init__.py", line 180, in from . import add_newdocs File "C:\Users\matt.heath\AppData\Local\Continuum\Anaconda3\lib\site-packages\numpy\add_newdocs.py", line 13, in from numpy.lib import add_newdoc File "C:\Users\matt.heath\AppData\Local\Continuum\Anaconda3\lib\site-packages\numpy\lib__init__.py", line 8, in from .type_check import * File "C:\Users\matt.heath\AppData\Local\Continuum\Anaconda3\lib\site-packages\numpy\lib\type_check.py", line 11, in import numpy.core.numeric as _nx File "C:\Users\matt.heath\AppData\Local\Continuum\Anaconda3\lib\site-packages\numpy\core__init__.py", line 14, in from . import multiarray ImportError: cannot import name 'multiarray'
and PyImport_ImportModule("numpy")
returns NULL
.
I can open other modules OK (e.g. PyRun_SimpleString("import chunk");
is fine) and import numpy
works just fine from Python in a console.
I am using Windows 10 and Python 3.5.
What can I do?
Upvotes: 8
Views: 1735
Reputation: 2573
This was the same as I experienced - although I used the precompiled python35_d.dll from the installer (ticked all of the boxes during the "customise install"). I also experienced similar problems with Python3.6 and 3.7 - so I suspect there may be a DLL pulled in which needs to have a "debug" variety.
I created a build configuration "release-with-symbols" based on the Release configuration but with symbols (debug information) enabled as described below:
https://learn.microsoft.com/en-us/cpp/build/how-to-debug-a-release-build?view=vs-2019
To debug a release build
Open the Property Pages dialog box for the project. For details, see Set C++ compiler and build properties in Visual Studio.
Click the C/C++ node. Set Debug Information Format to C7 compatible (/Z7) or Program Database (/Zi).
Expand Linker and click the General node. Set Enable Incremental Linking to No (/INCREMENTAL:NO).
Select the Debugging node. Set Generate Debug Info to Yes (/DEBUG).
Select the Optimization node. Set References to /OPT:REF and Enable COMDAT Folding to /OPT:ICF.
You can now debug your release build application. To find a problem, step through the code (or use Just-In-Time debugging) until you find where the failure occurs, and then determine the incorrect parameters or code.
If an application works in a debug build, but fails in a release build, one of the compiler optimizations may be exposing a defect in the source code. To isolate the problem, disable selected optimizations for each source code file until you locate the file and the optimization that is causing the problem. (To expedite the process, you can divide the files into two groups, disable optimization on one group, and when you find a problem in a group, continue dividing until you isolate the problem file.)
You can use /RTC to try to expose such bugs in your debug builds.
Upvotes: 0
Reputation: 493
Actually, this is only a problem in a debug build. I built python35_d.dll myself while the release dll was already compiled so maybe I had some weird setting in the make file or something. Anyway, I can live without debug, I guess.
Upvotes: 3