cython error compiling with print function parameters

房东的猫 提交于 2020-01-02 00:49:50

问题


when use cython to create helloworld.c from helloworld.pyx , this error occured:

    error compiling Cython file:
------------------------------------------------------------
...
print('hello world',end='')
                      ^
------------------------------------------------------------

p21.pyx:1:23: Expected ')', found '='

my command to create helloworld.c is:

cython3 --embed p21.pyx

回答1:


It looks like cython treats all prints as python 2 statements by default. In order to use the python 3 print function you need to import it from the future module:

from __future__ import print_function

print('hello world',end='')



回答2:


Cython is defaulting to Python 2 semantics. Set the language level to 3, which can be done with the following comment:

#cython: language_level=3

ref: https://cython.readthedocs.io/en/stable/src/reference/compilation.html#compiler-directives




回答3:


I don't know if this is still relevant, but in my case, with cython 0.23, to compile Python3 code you have to pass the flag -3. For example

cython -3 mycode.py


来源:https://stackoverflow.com/questions/19185338/cython-error-compiling-with-print-function-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!