Cython / Numpy - cimport numpy as np doesn't work

I'm trying to use cython to encode an important part of my code, but I'm having trouble importing numpy:

Setup.py

    from distutils.core import setup
    from distutils.extension import Extension
    from Cython.Distutils import build_ext
    import numpy as np

    e_m1 = Extension('OrderC', ['OrderC.pyx'])
    ext_mods = [e_m1]
    setup(name = 'OrderC/ListOrderC',  cmdclass = {'build_ext': build_ext}, 
          include_dirs = [np.get_include()], ext_modules = ext_mods)

      

OrderC.pyx:

    import numpy as np
    #cimport numpy as np  ## CRITICAL LINE (RUN WITHOUT IT)

    cdef class OrderC:
        cdef double _px

    def __init__(self, double px):
        self._px = px

    def printPx(self):
        print self._px

      

Home:

    import pyximport; 
    pyximport.install()

    from OrderC import OrderC
    oo = OrderC(9.95)
    oo.printPx()

      

When I comment out the critical line, the compilation of print warnings but gets executed, here is the warning:

    C:\Users\user\.pyxbld\temp.win32-2.7\Release\pyrex\OrderC.c: In function '__Pyx_RaiseArgtupleInvalid':
    C:\Users\user\.pyxbld\temp.win32-2.7\Release\pyrex\OrderC.c:1264:18: warning: unknown conversion type character 'z' in format [-Wformat=]
              (num_expected == 1) ? "" : "s", num_found);
              ^
    C:\Users\user\.pyxbld\temp.win32-2.7\Release\pyrex\OrderC.c:1264:18: warning: format '%s' expects argument of type 'char *', but argument 5 has type 'Py_ssize_t' [-Wformat=]
    C:\Users\user\.pyxbld\temp.win32-2.7\Release\pyrex\OrderC.c:1264:18: warning: unknown conversion type character 'z' in format [-Wformat=]
    C:\Users\user\.pyxbld\temp.win32-2.7\Release\pyrex\OrderC.c:1264:18: warning: too many arguments for format [-Wformat-extra-args]

      

But when I decompose the critical line, it doesn't work at all:

Building the settings file will give me:

    running build_ext
    cythoning OrderC.pyx to OrderC.c
    building 'OrderC' extension
    C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Python27_x86\lib\site-packages\numpy\core\include -IC:\Python27_x86\include -IC:\Python27_x86\PC -c OrderC.c -o build\temp.win32-2.7\Release\orderc.o
    In file included from C:\Python27_x86\lib\site-packages\numpy\core\include/numpy/ndarraytypes.h:1804:0,
             from C:\Python27_x86\lib\site-packages\numpy\core\include/numpy/ndarrayobject.h:17,
             from C:\Python27_x86\lib\site-packages\numpy\core\include/numpy/arrayobject.h:4,
             from OrderC.c:352:
    C:\Python27_x86\lib\site-packages\numpy\core\include/numpy/npy_1_7_deprecated_api.h:12:9: note: #pragma message: C:\Python27_x86\lib\site-packages\numpy\core\include/numpy/npy_1_7_deprecated_api.h(12) : Warning Msg: Using deprecated NumPy API, disable it by #defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
     #pragma message(_WARN___LOC__"Using deprecated NumPy API, disable it by " \
     ^
    OrderC.c: In function '__Pyx_RaiseArgtupleInvalid':
    OrderC.c:3828:18: warning: unknown conversion type character 'z' in format [-Wformat=]
              (num_expected == 1) ? "" : "s", num_found);
              ^
    OrderC.c:3828:18: warning: format '%s' expects argument of type 'char *', but argument 5 has type 'Py_ssize_t' [-Wformat=]
    OrderC.c:3828:18: warning: unknown conversion type character 'z' in format [-Wformat=]
    OrderC.c:3828:18: warning: too many arguments for format [-Wformat-extra-args]
    OrderC.c: In function '__Pyx_RaiseTooManyValuesError':
    OrderC.c:4065:18: warning: unknown conversion type character 'z' in format [-Wformat=]
              "too many values to unpack (expected %" CYTHON_FORMAT_SSIZE_T "d)", expected);
              ^
    OrderC.c:4065:18: warning: too many arguments for format [-Wformat-extra-args]
    OrderC.c: In function '__Pyx_RaiseNeedMoreValuesError':
    OrderC.c:4071:18: warning: unknown conversion type character 'z' in format [-Wformat=]
              index, (index == 1) ? "" : "s");
              ^
    OrderC.c:4071:18: warning: format '%s' expects argument of type 'char *', but argument 3 has type 'Py_ssize_t' [-Wformat=]
    OrderC.c:4071:18: warning: too many arguments for format [-Wformat-extra-args]
    In file included from C:\Python27_x86\lib\site-packages\numpy\core\include/numpy/ndarrayobject.h:26:0,
             from C:\Python27_x86\lib\site-packages\numpy\core\include/numpy/arrayobject.h:4,
             from OrderC.c:352:
    OrderC.c: At top level:
    C:\Python27_x86\lib\site-packages\numpy\core\include/numpy/__multiarray_api.h:1629:1: warning: '_import_array' defined but not used [-Wunused-function]
     _import_array(void)
     ^
    In file included from C:\Python27_x86\lib\site-packages\numpy\core\include/numpy/ufuncobject.h:317:0,
             from OrderC.c:353:
    C:\Python27_x86\lib\site-packages\numpy\core\include/numpy/__ufunc_api.h:241:1:         warning: '_import_umath' defined but not used [-Wunused-function]
     _import_umath(void)
     ^
    writing build\temp.win32-2.7\Release\OrderC.def
    C:\MinGW\bin\gcc.exe -shared -s build\temp.win32-2.7\Release\orderc.o build\temp.win32-2.7\Release\OrderC.def -LC:\Python27_x86\libs -LC:\Python27_x86\PCbuild -lpython27 -lmsvcr90 -o C:\Users\user\Desktop\Workspace\TestC\OrderC.pyd

      

So there is no fatal error other than many (strange) warnings.

Execution:

When I execute main it gives me:

    C:\Users\mmahfouda\.pyxbld\temp.win32-2.7\Release\pyrex\OrderC.c:352:31: fatal error: numpy/arrayobject.h: No such file or directory
     #include "numpy/arrayobject.h"
                           ^
    compilation terminated.
    Traceback (most recent call last):
      File "C:\Users\mmahfouda\Desktop\Workspace\TestC\MainTestC.py", line 4, in <module>
from OrderC import OrderC
      File "C:\Python27_x86\lib\site-packages\pyximport\pyximport.py", line 431, in load_module
language_level=self.language_level)
      File "C:\Python27_x86\lib\site-packages\pyximport\pyximport.py", line 209, in load_module
inplace=build_inplace, language_level=language_level)
      File "C:\Python27_x86\lib\site-packages\pyximport\pyximport.py", line 186, in build_module
reload_support=pyxargs.reload_support)
      File "C:\Python27_x86\lib\site-packages\pyximport\pyxbuild.py", line 104, in pyx_to_dll
dist.run_commands()
      File "C:\Python27_x86\lib\distutils\dist.py", line 953, in run_commands
self.run_command(cmd)
      File "C:\Python27_x86\lib\distutils\dist.py", line 972, in run_command
cmd_obj.run()
      File "C:\Python27_x86\lib\site-packages\Cython\Distutils\build_ext.py", line 163, in run
_build_ext.build_ext.run(self)
      File "C:\Python27_x86\lib\distutils\command\build_ext.py", line 337, in run
self.build_extensions()
      File "C:\Python27_x86\lib\site-packages\Cython\Distutils\build_ext.py", line 171, in build_extensions
self.build_extension(ext)
      File "C:\Python27_x86\lib\distutils\command\build_ext.py", line 496, in build_extension
depends=ext.depends)
      File "C:\Python27_x86\lib\distutils\ccompiler.py", line 574, in compile
self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
      File "C:\Python27_x86\lib\distutils\cygwinccompiler.py", line 166, in _compile
raise CompileError, msg
    ImportError: Building module OrderC failed: ["CompileError: command 'C:\\\\MinGW\\\\bin\\\\gcc.exe' failed with exit status 1\n"]

      

For information, my framework is: * Windows 7 * Pidev * Python 2.7 (32 bit)

Do you have the same error or have an idea why this simple program doesn't work?

Thank.

+3


source to share


1 answer


I am posting a solution I have ever suggested that worked for me:

According to him, the problem is compiling with pyximport when you use additional C libraries like numpy.

I changed the main file as follows:

from OrderC import OrderC
oo = OrderC(9.95)
oo.printPx()

      

Then



  • Compile with Cython:

    C: \ Python27_x86 \ Scripts \ cython.exe OrderC.pyx

  • Compile the c file with gcc (including additional libraries)

    C: \ MinGW \ bin \ gcc.exe -mdll -O -Wall -IC: \ Python27_x86 \ lib \ site-packages \ numpy \ core \ include -IC: \ Python27_x86 \ include -IC: \ Python27_x86 \ PC -c OrderC .c -o OrderC.o

  • Compile the c file with gcc (including additional libraries)

    C: \ MinGW \ bin \ gcc.exe -shared -s OrderC.o -LC: \ Python27_x86 \ libs -LC: \ Python27_x86 \ PCbuild -lpython27 -lmsvcr90 -o C: \ Users \ user \ Desktop \ Workspace \ TestC \ OrderC.pyd

Then it works.

Note:

  • step 2 generates warnings but works

  • make sure to add the OrderC path to your PATH environment variable

+1


source







All Articles