我正在尝试使用cython编写代码的关键部分,但是我无法导入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
主要:
import pyximport;
pyximport.install()
from OrderC import OrderC
oo = OrderC(9.95)
oo.printPx()
当我评论关键线,编译打印警告但运行时,这里是警告:
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]
但是,当我退出关键线时,它根本不起作用:
构建设置文件会给我:
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
所以没有严重的错误,但很多(怪异的)警告。
执行:
当我执行main时,它会给我:
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"]
有关信息,我的框架是: * Windows 7的 * Pydev * Python 2.7(32位)
您是否有同样的错误或想知道为什么这个简单的程序不起作用?
谢谢。
答案 0 :(得分:2)
我发布了对我有用的解决方案:
正如他所说,当你使用额外的C库作为numpy时,问题来自使用pyximport进行编译。
我修改了主文件如下:
from OrderC import OrderC
oo = OrderC(9.95)
oo.printPx()
然后,
使用Cython编译:
C:\ Python27_x86 \ Scripts \ cython.exe OrderC.pyx
使用gcc(包括额外的库)编译c文件
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
使用gcc(包括额外的库)编译c文件
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
然后它有效。
注意:
第2步生成警告,但有效
确保在PATH环境变量中添加OrderC路径