Skip to content Skip to sidebar Skip to footer

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 . I use cython in some parts of the code, hence the package has both .py and .pyx fi

Solution 1:

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

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). There are a lot of preprocessor #if PY_MAJOR_VERSION >= 3 sections in the generated C code to ensure this.

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. There may be small, specific issues you have to work round though.


Post a Comment for "Porting Cython Files From Python2 To Python3 With 2to3"