我正在尝试使用我编写的名为 compileToPyd
的自定义模块(它只处理与编译相关的各种命令)。
尝试编译一个简单文件时出现许多错误:
path\lib>C:\Python27\python.exe "main_setup.py" build
running build
running build_ext
cythoning main.pyx to main.c
Error compiling Cython file:
------------------------------------------------------------
...
?# main.pyd
^
------------------------------------------------------------
main.pyx:1:0: Unrecognized character
building 'main' extension
c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN\cl.exe /c /nologo /Ox
/MD /W3 /GS- /DNDEBUG -IC:\Python27\include -IC:\Python27\PC /Tcmain.c /Fobuild\
temp.win32-2.7\Release\main.obj
main.c
main.c(1) : fatal error C1189: #error : Do not use this file, it is the result
of a failed Cython compilation.
error: command '"c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN\cl.ex
e"' failed with exit status 2
python\lib>"gcc" "main.c" -o "main.pyd" -shared -I"C:\Python27\include" -L"C:\Python27\libs" -lpython
27
main.c:1:2: error: #error Do not use this file, it is the result of a failed Cyt
hon compilation.
#error Do not use this file, it is the result of a failed Cython compilation.
文件本身非常简单:
# main.pyd
# author name
# January 2014
class AuthenticationError(Exception):
pass
def main():
'''Main process'''
import authenticate
if authenticate.authenticate():
import some_module
some_module.main()
else:
raise AuthenticationError("Not Authenticated")
它不是可执行文件,需要导入。该编译适用于其他文件,为什么这个不行呢?谢谢!
最佳答案
这个:
?# main.pyd
加上您的文件:
# main.pyd
表示文件开头有不可打印的字符,编辑器看不到。这通常是由于您的编辑器在文件的开头插入了字节顺序标记,以指示保存文件时使用了哪种编码。
此 BOM 在编辑器中不可见,但当然会存在,并且对于任何无法正确进行解码的软件,都会看起来很奇怪且不合适。
进入 cython,它就是这样做的。
因此,要解决此问题,您需要删除该 BOM。
在 Visual Studio 中,您可以通过打开文件并使用文件菜单项“高级保存选项”轻松完成此操作:
然后在对话框中您要确保选择不包含“带有签名”一词的编码:
此处显示的编码“Unicode(带有签名的UTF-8)-代码页65001”当然不适合您使用。我会尝试第一个项目“西欧 (Windows) - 代码页 1252”。
请注意,编码更改在某些情况下可能会更改您的文件。如果您选择了错误的编码,诸如版权符号和重音字符之类的内容可能并且将会失去其表示形式。因此,对于此类字符,最好将它们作为转义字符嵌入到源代码中。
关于python - GCC 未编译 Cython 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21220677/