Python 3

 

Migrating to Python 3

Since the Python 3 series was first released in 2008, the core team has consistently produced releases that have moved the language forward. If you work for an organization that has built their applications in Python 2.7 (or older), you know that the Python Software Foundation no longer supports or updates Python 2. While your applications may be working fine today, they will become more vulnerable over time as security issues are found. As a result, you should be evaluating whether it is time to migrate to Python 3.

In order to assist with the migration, many large organizations run Python 2.7 in parallel. Since you can safely install both ActivePython 2.7 and ActiveState Python 3 side-by-side on your computer, you can experiment with Python 3 while still using Python 2 for your current code.

Download ActiveState Python to help with your Python migration project. Python is available for Windows, Linux, and macOS.

DOWNLOAD PYTHON 3


Python 3 Code Changes

Migrating to Python 3 is not a simple “push button” upgrade because Python 3 is not backward compatible with Python 2. You will have to make code-level changes. Also keep in mind that while most of the open source libraries you used to build your application on Python 2 have been ported to Python 3, they may not work in exactly the same way. Testing will be key.

Python 3 also offers a number of significant improvements over Python 2, including:

  • Unicode support
  • Floating point operations 
  • Integer division
  • Asynchronous support
  • Better exception handling
  • Ability to mix tabs and spaces

Python 3 also gives you function annotations and range memory objects which are significantly more memory efficient. Some of the code-level changes are highlighted below:

“PRINT” is a function

The print statement has been replaced with a print() function, with keyword arguments to replace most of the special syntax of the old print statement.

Old: print "The answer is", 2*2 
New: print("The answer is", 2*2) 
Old: print >>sys.stderr, "fatal error" 
New: print("fatal error", file=sys.stderr) 

Unicode Changes

How Python handles text vs. data has changed considerably. All text is Unicode (represented by the str type), all binary data (e.g. 8-bit strings, encoded Unicode) is held in the new bytes type.

Syntax Changes

Python 3 supports function argument and return value annotations. Functions can declare keyword-only arguments. Extended iterable unpacking allows code like:

a, b, *rest = some_sequence
*rest, last = some_sequence

Other syntax changes:

d = {k: v for k, v in stuff} # dictionary comprehensions 
s = {"peter", "paul", "mary"} # set literals 
mode = 0o755 # octal literals 
mask = 0b01001111 # binary literals 
data = b"marker" # bytes literals 

Much More

There are many more changes in Python 3. Resources for Python 3:


Porting to Python 3

The currently suggested path for migrating from Python 2 to 3 is to:

  1. Upgrade your Python 2 code base to at least Python 2.6. ActiveState recommends updating to the latest version of Python 2, ActivePython 2.7.18.
  2. Learn how Python 3 differs from Python 2
  3. Ensure you have a robust test suite in place
  4. Run an automation tool like 2to3, modernize or python-future 
  5. Perform manual code updates for key functions like division, CSV parsing, print statements, lists, relative imports, etc

For more information:

Developers worldwide rely on ActivePython’s completeness and ease-of-use, while corporate users protect their infrastructure and stay competitive with quality-assured ActivePython business solutions.

Download Python 3

Scroll to Top