21

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
mohammad
  • 241
  • 2
  • 8

3 Answers3

21

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

user2357112
  • 260,549
  • 28
  • 431
  • 505
YangZi
  • 211
  • 2
  • 4
19

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='')
uzumaki
  • 1,743
  • 17
  • 32
8

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
AkiRoss
  • 11,745
  • 6
  • 59
  • 86
  • In my opinion, this is the best answer, because it does not require you to modify any source file. – tfpf Dec 07 '20 at 13:57
  • 1
    In my opinion this is not the best answer because the language level to use is a property of the source file. It's much more robust to put it in the source file and then you'll never forget it on the command line – DavidW May 20 '22 at 17:44
  • I agree, not the best option even if I suggested it as a possible solution. – AkiRoss Jun 28 '22 at 18:56