Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't import numpy from C

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 numpyworks just fine from Python in a console.

I am using Windows 10 and Python 3.5.

What can I do?

like image 680
MatthewJohnHeath Avatar asked Nov 01 '16 12:11

MatthewJohnHeath


People also ask

Why is NumPy not importing?

The Python "ModuleNotFoundError: No module named 'numpy'" occurs when we forget to install the numpy module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install numpy command.

Can you use NumPy in C?

NumPy is written in C, and executes very quickly as a result. By comparison, Python is a dynamic language that is interpreted by the CPython interpreter, converted to bytecode, and executed. While it's no slouch, compiled C code is always going to be faster.

Why NumPy module is not working?

The error “No module named numpy ” will occur when there is no NumPy library in your environment i.e. the NumPy module is either not installed or some part of the installation is incomplete due to some interruption.


1 Answers

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.

like image 51
MatthewJohnHeath Avatar answered Oct 06 '22 03:10

MatthewJohnHeath