简体   繁体   English

使用2to3将cython文件从python2移植到python3

[英]Porting cython files from python2 to python3 with 2to3

I have a python package which was developed under python2.7, but I need to port it to python3.6 . 我有一个python包在python2.7下开发,但我需要将它移植到python3.6。 I use cython in some parts of the code, hence the package has both .py and .pyx files. 我在代码的某些部分使用cython,因此该包具有.py.pyx文件。

I tried the 2to3 command, but I got an error that I couldn't neither understand nor solve. 我尝试了2to3命令,但是我得到了一个我既不理解也不解决的错误。

Example: I have the following test.pyx file 示例:我有以下test.pyx文件

# cython: profile=False
cimport cython

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.profile(False)
cpdef sillyfunction():
    print 'Thank you for your kind help'
    return

and I run 2to3 test.pyx . 我运行2to3 test.pyx What I obtain is: 我得到的是:

user@machine:~$ 2to3 test.pyx
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Can't parse test.pyx: ParseError: bad input: type=1, value=u'cython', context=(u' ', (2, 8))
RefactoringTool: No files need to be modified.
RefactoringTool: There was 1 error:
RefactoringTool: Can't parse test.pyx: ParseError: bad input: type=1, value=u'cython', context=(u' ', (2, 8))

You shouldn't need to do anything. 你不应该做任何事情。 Cython accepts an argument language_level (see http://cython.readthedocs.io/en/latest/src/reference/compilation.html#compiler-directives ) which controls where it interprets the code as Python 2 or Python 3 (for example print as a function or as a statement). Cython接受一个参数language_level (参见http://cython.readthedocs.io/en/latest/src/reference/compilation.html#compiler-directives ),它控制将代码解释为Python 2或Python 3的位置(例如print作为一种功能或一种陈述)。

Whichever you do the code it generates should be compilable to use with Python 2 or Python 3 (this is determined by what headers you include, which is largely arranged by the build process). 无论你做什么,它生成的代码都应该可以编译为与Python 2或Python 3一起使用(这取决于你所包含的头文件,这主要由构建过程安排)。 There are a lot of preprocessor #if PY_MAJOR_VERSION >= 3 sections in the generated C code to ensure this. 生成的C代码中有很多预处理器#if PY_MAJOR_VERSION >= 3部分来确保这一点。

I suspect that there are some limitations on this compatibility, and I certainly wouldn't expect all Python 3 features to work perfectly when compiled against Python 2, but as a general rule you should be able to take your existing Cython code, run Cython on it with language_level=2 (the default) and then compile it using the Python 3 headers/libraries (which setup.py should take care of by default) and it should work. 我怀疑这种兼容性存在一些限制,我当然不希望所有Python 3功能在针对Python 2编译时都能正常工作,但作为一般规则,您应该能够使用现有的Cython代码,运行Cython on它使用language_level=2 (默认值),然后使用Python 3头文件/库(默认情况下setup.py应该处理)编译它,它应该可以工作。 There may be small, specific issues you have to work round though. 您可能需要解决一些小问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM