如何在Cython的setup.py中指定Python 3源? [英] How to specify Python 3 source in Cython's setup.py?

查看:230
本文介绍了如何在Cython的setup.py中指定Python 3源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照本教程 http://docs.cython.org/src/tutorial/cython_tutorial.html#cython-hello-world

我创建了helloworld .pyx

I created helloworld.pyx

print("Hello World")

和setup.py:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules = cythonize("helloworld.pyx")
)

如何更改setup.py以指定源是Python 3,而不是本教程中的Python 2?如果我从命令行调用 cython命令,它将接受 -3 选项。但是,如果我使用本教程中所示的 python setup.py build_ext --inplace 进行编译,如何指定Python 3源?对于Hello World程序而言,它可能无关紧要,但是随着我开始在实际项目中使用Cython时,它就很重要。

How can I change setup.py to specify that my source is Python 3, rather than Python 2 like in the tutorial? If I invoke "cython" command from the command line, it accepts -3 option. But if I compile with python setup.py build_ext --inplace like shown in the tutorial, how do I specify Python 3 source? It may not matter much for a Hello World program, but will matter as I start using Cython for real projects.

非常感谢!

推荐答案

一个可以通过 language_level 作为选项,用于 cythonize 中的功能 setup.py -script:

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"
             ) 

另一种可能的语法是

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




上面的内容可能比添加


The above might be more convenient than to add

#cython: language_level=3

到项目中的每个pyx文件,这可能是必要的,因为自Cython 0.29起,如果 language_level 未明确设置

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指令'language_level'未设置
,现在使用2(Py2)。这将在以后的版本中更改!文件:
XXXXXX.pyx

tree = Parsing.p_module(s,pxd,full_module_name)




因为 language_level 是全局设置,所以装饰器


Because language_level is a global setting, the decorator

cimport cython

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

甚至不会被cythonized。

will not even be cythonized.

这篇关于如何在Cython的setup.py中指定Python 3源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆