Python 3.x 使用打印函数参数编译cython时出错
使用cython从helloworld.pyx创建helloworld.c时,出现以下错误:Python 3.x 使用打印函数参数编译cython时出错,python-3.x,cython,Python 3.x,Cython,使用cython从helloworld.pyx创建helloworld.c时,出现以下错误: error compiling Cython file: ------------------------------------------------------------ ... print('hello world',end='') ^ -----------------------------------------------------
error compiling Cython file:
------------------------------------------------------------
...
print('hello world',end='')
^
------------------------------------------------------------
p21.pyx:1:23: Expected ')', found '='
我创建helloworld.c的命令是:
cython3 --embed p21.pyx
看起来cython在默认情况下将所有打印处理为python 2语句。要使用python 3打印功能,您需要从future模块导入它:
from __future__ import print_function
print('hello world',end='')
我不知道这是否仍然相关,但在我的例子中,对于cython 0.23,要编译Python3代码,必须传递标志
-3
。比如说
cython -3 mycode.py
Cython默认为Python 2语义。将语言级别设置为3,可通过以下注释完成:
#cython: language_level=3
ref:在我看来,这是最好的答案,因为它不需要修改任何源文件。