Differences between revisions 30 and 31
Revision 30 as of 2019-06-02 09:08:05
Size: 4039
Editor: chapoton
Comment: refactoring
Revision 31 as of 2019-08-22 11:47:12
Size: 4008
Editor: chapoton
Comment:
Deletions are marked like this. Additions are marked like this.
Line 15: Line 15:
||<#FF6347>Warning: || The Python 3 version of SageMath is almost fully functional but still at experimental stage. You may still encounter a few remaining issues.|| ||<#FF6347>Warning: || The Python 3 version of SageMath is essentially fully functional but not yet the default distributed version.||

SageMath is currently using Python2, but is moving towards Python3.

* So far, you can only get binaries for SageMath with Python2. This may change soon (maybe after release 8.8).

* You can already build and use SageMath with Python3, as follows.

make configure
./configure --with-python=3
make build

Beware that you will need to call the second line again if you ever call "make distclean".

Warning:

The Python 3 version of SageMath is essentially fully functional but not yet the default distributed version.

Advice on writing Python3-compatible code

To make the transition possible, it is required that the code must be as compatible as possible with both Python2 and Python3. Let us describe a few important points.

Do not use xrange

You can use xrange only inside Cython files (*.pyx). If you need an iterator range, you should add "from six.moves import range" in the top import section of your *.py file.

In the doctests, make sure that your doctest will still work when range becomes an iterator.

Do not use it.next() to advance iterators

Use instead the syntax "next(it)" which is compatible with both Python2 and Python3.

Do not use .itervalues, .iteritems or .iterkeys

You can either use .values, .items and .keys (that are not iterators in Python2) or add "from six import itervalues" and use "itervalues(something)"

Do not use cmp()

The cmp global function is gone in Python3 and must be avoided completely.

SageMath now has a "richcmp" function that can sometimes replace "cmp".

Behaviour of print

The behaviour of print differs in Python 2 and in Python 3.

SageMath interface is currently using the Python2 syntax for print, but SageMath code is written in such a way that it is also Python3 compatible. It is therefore required to use print in a way that is compatible with both Python 2 and Python 3.

In the Python 3 syntax, print is a function.

The easiest way to achieve this compatibility is to always use print(...) with a single string inside the parentheses.

For example, print("He has eaten {} bananas".format(10)) will work in both Python 2 and Python 3 and give the same result.

Here below are some instructions for going from Python 2-only print to Python 3-only print. The Python 3 syntax can be used right now if you add the line

from __future__ import print_function

as the first code line of your python files.

To convert print from Python 2 to Python 3, you basically need to add parentheses, and write print("x") instead of print "x".

Here is a conversion table to help you adapt your code in more complex cases:

Python 2

Python 3

print a

print(a)

print a, b, c

print(a, b, c)

print x,

print(x, end=" ")

print >>sys.stderr, x

print(x, file=sys.stderr)

Author/Reviewer's checklist

Check the result of the patchbot Python3 compatibility plugins named "next_method", "oldstyle_print", "python3" and "raise_statements" (for example, see the list on the right at this patchbot report).

Otherwise, check Python 3 syntax errors in every file changed in the current branch:

git diff --name-only develop your-new-branch | xargs -n 1 python3 -m py_compile

Check Python 3 syntax errors in every Python file of Sage library (see this thread):

find src/sage -name '*.py' | xargs -n 1 python3 -m py_compile 

If possible, include the following imports to the modules you are adding or working on:

from __future__ import division, absolute_import, print_function

The command pyflakes, when installed with pip3, is also very useful to detect incompatibilities with Python3.

Python3-compatible code (last edited 2020-09-12 07:11:42 by chapoton)