2017-06-20 300 views
0

我有一個運行得太慢的巨大Python代碼。我不想用C重寫我的整個代碼,所以我做了一些研究,並且遇到了Cython。我被告知我的Python代碼保存爲.pyx並創建一個setup.py文件,將「cytonize」我的代碼,然後將其編譯成C.Cython文件無法編譯

然而,當我鍵入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):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字符。我應該添加什麼?

+2

'maido-cython' =>'maido_cython'。文件名必須是有效的模塊名稱。 –

+1

另外,請勿使用相同的基本名稱命名'.pyx'和'.py'文件。 –

+0

謝謝,我編輯了我的問題,因爲我有另一個問題。 –

回答

3

您可以完美執行一個.py*文件,其基數名稱如a-b但您不能導入它。

import a-b 

無效,因爲a-b不是有效蟒標識符(解釋爲「負B」)。您的cythonize過程也一樣,並且消息足夠清晰。

修復:將maido-cython.pyx重命名爲有效的python名稱,例如maido_cython.pyx

編輯:你的另一個問題,它的另一個經典的問題:

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

\U被解釋爲Unicode轉義。使用像這樣的原始前綴:

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

你能回答我編輯過的帖子嗎?我有另一個問題。 –

+1

@LoïcPoncin完成(這是另一個無關的問題)。請不要進一步編輯您的問題。想想這個頁面的未來讀者。 –