我正在尝试将一些.py
文件编译成.pyd
,使用我编写的名为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")
它不是可执行文件,而是要导入。编译适用于其他文件,为什么不这个呢?谢谢!
答案 0 :(得分:2)
此:
?# main.pyd
加上你的文件:
# main.pyd
表示文件在开头有不可打印的字符,编辑器不可见。这通常是由编辑器在文件的最开头插入一个字节顺序标记引起的,以指示保存时使用的编码。
这个BOM在编辑器中不可见,但当然会在那里,对于任何没有正确解码的软件,它看起来会很奇怪而且不合适。
输入cython,就是这样。
因此,要解决此问题,您需要删除该BOM。
在Visual Studio中,您可以通过打开文件并使用文件菜单项“高级保存选项”轻松完成此操作:
然后在对话框中,您要确保选择不包含“with signature”字样的编码:
此处显示的编码,“Unicode(带签名的UTF-8) - 代码页65001”当然是错误的,供您使用。我会尝试第一个项目,“西欧(Windows) - 代码页1252”。
请注意,在某些情况下,编码更改可能会更改您的文件。如果您选择了错误的编码,那么像版权符号和重音字符这样的东西会丢失它们的表示。因此,对于此类字符,最好将它们作为转义字符嵌入源代码中。