1

我有一个运行速度太慢的巨大 Python 代码。我不想用 C 重写我的整个代码,所以我做了一些研究,然后遇到了 Cython。有人建议我将我的 Python 代码保存为 .pyx 并创建一个 setup.py 文件,该文件将“细胞化”我的代码,然后将其编译成 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字符。我应该添加什么?

4

1 回答 1

4

您可以完美地执行.py*具有基数名称的文件,a-b但您无法导入它。

import a-b

无效,因为a-b它不是有效的 Python 标识符(解释为“a 减 b”)。您的 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"
于 2017-06-20T20:47:16.290 回答