使用 Cython 时,“cimport numpy”会引发错误

发布于 2024-12-13 12:17:17 字数 2214 浏览 1 评论 0原文

我试图从 .pyx 文件将 cimport NumPy 导入 Python 2.7 shell,但它一直给我同样的错误:

我做了一个 .pyx 文件名为 numpyx 只是为了看看它是否是我正在运行的更大代码的一部分,该文件包含:

cimport numpy as np
a = np.arange(0,10)
print 'a= ',a

我每次都会收到以下错误:

Traceback (most recent call last):
  File "<pyshell#82>", line 1, in <module>
    import numpyx
  File "C:\Users\Scott\AppData\Roaming\Python\Python27\site-packages\pyximport       \pyximport.py", line 335, in load_module
    self.pyxbuild_dir)
  File "C:\Users\Scott\AppData\Roaming\Python\Python27\site-packages\pyximport\pyximport.py", line 183, in load_module
    so_path = build_module(name, pyxfilename, pyxbuild_dir)
  File "C:\Users\Scott\AppData\Roaming\Python\Python27\site-packages\pyximport\pyximport.py", line 167, in build_module
    reload_support=pyxargs.reload_support)
  File "C:\Users\Scott\AppData\Roaming\Python\Python27\site-packages\pyximport\pyxbuild.py", line 85, in pyx_to_dll
    dist.run_commands()
  File "C:\Python27\lib\distutils\dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Users\Scott\AppData\Roaming\Python\Python27\site-packages\Cython\Distutils\build_ext.py", line 135, in run
_    build_ext.build_ext.run(self)
  File "C:\Python27\lib\distutils\command\build_ext.py", line 340, in run
    self.build_extensions()
  File "C:\Users\Scott\AppData\Roaming\Python\Python27\site-packages\Cython\Distutils\build_ext.py", line 143, in build_extensions
    self.build_extension(ext)
  File "C:\Python27\lib\distutils\command\build_ext.py", line 499, in build_extension
    depends=ext.depends)
  File "C:\Python27\lib\distutils\ccompiler.py", line 624, in compile
    self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
  File "C:\Python27\lib\distutils\cygwinccompiler.py", line 166, in _compile
    raise CompileError, msg
ImportError: Building module failed: ["CompileError: command 'gcc' failed with exit status 1\n"]

我不明白为什么它不起作用,因为它编译了只要 cimport 不在其中,.pyx 文件就可以正常工作。

如果有人能阐明这一点,那就太好了!

I am trying to cimport NumPy into a Python 2.7 shell from a .pyx file, but it keeps giving me the same error:

I made a .pyx file called numpyx just to see if it was part of the bigger code I was running, the file contains:

cimport numpy as np
a = np.arange(0,10)
print 'a= ',a

I get the following error every time:

Traceback (most recent call last):
  File "<pyshell#82>", line 1, in <module>
    import numpyx
  File "C:\Users\Scott\AppData\Roaming\Python\Python27\site-packages\pyximport       \pyximport.py", line 335, in load_module
    self.pyxbuild_dir)
  File "C:\Users\Scott\AppData\Roaming\Python\Python27\site-packages\pyximport\pyximport.py", line 183, in load_module
    so_path = build_module(name, pyxfilename, pyxbuild_dir)
  File "C:\Users\Scott\AppData\Roaming\Python\Python27\site-packages\pyximport\pyximport.py", line 167, in build_module
    reload_support=pyxargs.reload_support)
  File "C:\Users\Scott\AppData\Roaming\Python\Python27\site-packages\pyximport\pyxbuild.py", line 85, in pyx_to_dll
    dist.run_commands()
  File "C:\Python27\lib\distutils\dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Users\Scott\AppData\Roaming\Python\Python27\site-packages\Cython\Distutils\build_ext.py", line 135, in run
_    build_ext.build_ext.run(self)
  File "C:\Python27\lib\distutils\command\build_ext.py", line 340, in run
    self.build_extensions()
  File "C:\Users\Scott\AppData\Roaming\Python\Python27\site-packages\Cython\Distutils\build_ext.py", line 143, in build_extensions
    self.build_extension(ext)
  File "C:\Python27\lib\distutils\command\build_ext.py", line 499, in build_extension
    depends=ext.depends)
  File "C:\Python27\lib\distutils\ccompiler.py", line 624, in compile
    self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
  File "C:\Python27\lib\distutils\cygwinccompiler.py", line 166, in _compile
    raise CompileError, msg
ImportError: Building module failed: ["CompileError: command 'gcc' failed with exit status 1\n"]

I don't understand why it won't work, since it compiles .pyx files fine as long as cimport isn't in them.

If anyone could shed some light on this it would be great!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

开始看清了 2024-12-20 12:17:17

那么你要找的函数可以在 python 包中找到。
(我在 numpy.pxd 中没有找到对 linspace 的引用)

我通常会按照以下方式做一些事情:

import numpy as np
cimport numpy as cnp

def foo(double x):
    cdef cnp.ndarray[cnp.float64_t, ndim=1] y = np.linspace(0, 10, 11)
    # do whatever
    return y

Well the function your looking for is available in the python package.
(I find no reference to linspace in numpy.pxd)

I usually do something along these lines:

import numpy as np
cimport numpy as cnp

def foo(double x):
    cdef cnp.ndarray[cnp.float64_t, ndim=1] y = np.linspace(0, 10, 11)
    # do whatever
    return y
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文