簡體   English   中英

使用2to3將cython文件從python2移植到python3

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

我有一個python包在python2.7下開發,但我需要將它移植到python3.6。 我在代碼的某些部分使用cython,因此該包具有.py.pyx文件。

我嘗試了2to3命令,但是我得到了一個我既不理解也不解決的錯誤。

示例:我有以下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

我運行2to3 test.pyx 我得到的是:

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

你不應該做任何事情。 Cython接受一個參數language_level (參見http://cython.readthedocs.io/en/latest/src/reference/compilation.html#compiler-directives ),它控制將代碼解釋為Python 2或Python 3的位置(例如print作為一種功能或一種陳述)。

無論你做什么,它生成的代碼都應該可以編譯為與Python 2或Python 3一起使用(這取決於你所包含的頭文件,這主要由構建過程安排)。 生成的C代碼中有很多預處理器#if PY_MAJOR_VERSION >= 3部分來確保這一點。

我懷疑這種兼容性存在一些限制,我當然不希望所有Python 3功能在針對Python 2編譯時都能正常工作,但作為一般規則,您應該能夠使用現有的Cython代碼,運行Cython on它使用language_level=2 (默認值),然后使用Python 3頭文件/庫(默認情況下setup.py應該處理)編譯它,它應該可以工作。 您可能需要解決一些小問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM