How to specify Python 3 source in Cython's setup.py?

后端 未结 3 1750
孤城傲影
孤城傲影 2020-12-01 06:04

I am trying to do a "Hello World" program in Cython, following this tutorial http://docs.cython.org/src/tutorial/cython_tutorial.html#cython-hello-world

I c

3条回答
  •  情书的邮戳
    2020-12-01 06:52

    One can pass language_level as an option to the cythonize-function in the setup.py-script:

    extensions = cythonize(
                   extensions, 
                   compiler_directives={'language_level' : "3"})   # or "2" or "3str"
                 ) 
    

    Another possible syntax is

    extensions = cythonize(extensions, language_level = "3")
    

    The above might be more convenient than to add

    #cython: language_level=3
    

    to every pyx-file in the project, which might become necessary because since Cython 0.29 there is a warning, if the language_level isn't set explicitly:

    /Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: XXXXXX.pyx
    tree = Parsing.p_module(s, pxd, full_module_name)


    Because language_level is a global setting, the decorator

    cimport cython
    
    @cython.language_level("3")
    def do_something():
        pass
    

    will not even be cythonized.

提交回复
热议问题