Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

It is possible to write code that works for both major versions of Python, and it is the recommended strategy for maximum compatibility and flexibility. See the following page for a comprehensive list of compatible idioms. The easiest way to make sure your python 2 code can run on python 3 is to use automatic conversion tools such as futurize. Even if they are not perfect in all cases, they should take care of adapting the vast majority of your code base without too many bugs introduced. Futurize will also generate code that is compatible with both editions. See the quick start guide for instructions on how to use this tool.

Additionally, you may want to have a look at other tools and resources such as:

  • 2to3: it comes with the Python distribution, and it will attempt to convert 2.x source code into 3, but it may not generate code which is compatible with both.
  • Can I Use Python 3: This script takes in a set of dependencies and then figures out which of them are holding you up from porting to Python 3.
  • Six: a Python 2 and 3 compatibility library. It provides utility functions for smoothing over the differences between the Python versions with the goal of writing Python code that is compatible on both Python versions. See also nine,  which turns six upside down. You write your code using Python 3 idioms – as much as possible –, and it is the Python 2 “version” that is patched.
  • Supporting Python 3: An in-depth guide is a free, open source eBook that guides you through the process of adding Python 3 support, from choosing a strategy to solving your distribution issues. Using plenty of code examples, it guides you across the hurdles and shows you the new Python features.

Python 3 at ECMWF

Info

Both python and python3 modules can be loaded at the same time.

Python3 module IS NOT loaded by default, so do not forget to load it before running your scripts!

...