Python 2 - Python 3

Which Python Version ?

Warning

Python 3 is strongly recommended for any new development.

As of January 2020, Python 2 has reached End Of Life status, meaning it will receive no further updates or bugfixes, including for security issues. Many frameworks and other add on projects are following a similar policy. As such, we can only recommend learning and teaching Python 3. ^1

Recommendations

  • Use the same version as the Python code under test

  • Alternative use as minimum CPython 3.7, ideally 3.11 and if possible the latest stable version

  • Document in your test project the recommended minimum version of Python

Main Differences

Python 2

Python 3

Strings stored in ASCII

Python 3: Strings are Unicode by default

Rounds calculation to the nearest number: 5/2 = 2

Improved integer division: 5/2 = 2.5

print “Hello”

print(“Hello”)

except NameError, err:

except NameError as err:

xrange

range

raw_input

input

Migrating from Python 2 to 3

There are two main tools you can use to automatically update your code to Python 3 while keeping it compatible with Python 2: future and modernize.

Each of these tools behaves somewhat differently: future works to make Python 3 idioms and best practices exist in Python 2, while modernize aims for a Python 2/3 subset of Python that uses the Python six module to improve compatibility.^2

References