Cython文件无法编译

时间:2017-06-20 20:36:40

标签: python cython cythonize

我有一个运行速度太慢的巨大Python代码。我不想用C语言重写我的整个代码,所以我做了一些研究,我遇到了Cython。我被建议将我的Python代码保存为.pyx并创建一个setup.py文件,该文件将对我的代码进行“cytonize”,然后将其编译为C.

但是当我输入C:/Users/loic/Documents/maido-cython/setup.py build_ext –inplace时,我收到错误:

Compiling C:/Users/loic/Documents/maido-cython/maido-cython.pyx because it changed.
[1/1] Cythonizing C:/Users/loic/Documents/maido-cython/maido-cython.pyx

Error compiling Cython file:
------------------------------------------------------------
[unprintable code]
------------------------------------------------------------

Documents\maido-cython\maido-cython.pyx:1:0: 'maido-cython' is not a valid module name
Traceback (most recent call last):
  File "C:\Users\loic\Documents\maido-cython\setup.py", line 9, in <module>
    ext_modules = cythonize("C:/Users/loic/Documents/maido-cython/maido-cython.pyx"),
  File "C:\Python27\lib\site-packages\Cython\Build\Dependencies.py", line 934, in cythonize
    cythonize_one(*args)
  File "C:\Python27\lib\site-packages\Cython\Build\Dependencies.py", line 1056, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: C:/Users/loic/Documents/maido-cython/maido-cython.pyx

我使用的代码(maido-cython.py)和4个numpy文件(******_maido_856x512.npy)(不用担心我将其上传到我自己的Google帐户中):https://drive.google.com/drive/folders/0B7P95aWmH4DUek9xcEROb0FQR0E

以下是我在设置文件中写的内容:

try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

from Cython.Build import cythonize

setup(name = "graphique",
      ext_modules = cythonize("C:/Users/loic/Documents/maido-cython/maido-cython.pyx"),
      )

有人可以告诉我,我做错了吗?

修改

我接受了建议并更改了文件。我还更改了上传的链接:https://drive.google.com/drive/folders/0B7P95aWmH4DUcFY0X3JycGlacDQ

当我输入C:/Users/loic/Documents/maido/setup.py build_ext –inplace与冒号字符“:”

时,我有另一个问题
Compiling C:\Users\loic\Documents\maido\maido_cython.pyx because it changed.
[1/1] Cythonizing C:\Users\loic\Documents\maido\maido_cython.pyx

Error compiling Cython file:
------------------------------------------------------------
...


def metFeuForet():
    """ propage le feu a toute la foret pour N etape a l'aide de la fonction propageFeu """

    path = "C:\Users\loic\Documents\maido"
             ^
------------------------------------------------------------

Documents\maido\maido_cython.pyx:258:14: Invalid unicode escape '\U'

Traceback (most recent call last):
  File "C:\Users\loic\Documents\maido\setup.py", line 9, in <module>
    ext_modules = cythonize("C:\Users\loic\Documents\maido\maido_cython.pyx"),
  File "C:\Python27\lib\site-packages\Cython\Build\Dependencies.py", line 934, in cythonize
    cythonize_one(*args)
  File "C:\Python27\lib\site-packages\Cython\Build\Dependencies.py", line 1056, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: C:\Users\loic\Documents\maido\maido_cython.pyx

我没有找到相应的unicode字符。我应该添加什么?

1 个答案:

答案 0 :(得分:3)

您可以使用基数名称.py*完美地执行a-b文件,但不能导入

import a-b

不是有效的,因为a-b不是有效的python标识符(解释为&#34; a减去b&#34;)。您的cythonize过程也是如此,'maido-cython' is not a valid module name消息足够清晰。

修复:将maido-cython.pyx重命名为有效的python名称,例如maido_cython.pyx

编辑:对于你的另一个问题,这是另一个经典问题:

path = "C:\Users\loic\Documents\maido"

\U被解释为unicode转义。使用这样的原始前缀:

path = r"C:\Users\loic\Documents\maido"