问题
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