Python 3 is not backwards compatible with Python 2, so your code may need to be adapted.
Please start migrating your existing your existing Python 2 code to Python 3.
Python 2 series End Of Life is set to 1st of January 2020.
Python 2.7 was released on July 2010. Being the last of the 2.x series, 2.7 has had an extended period of maintenance. Specifically, 2.7 will receive bugfix support until January 1, 2020. After the last release, 2.7 will receive no support.
Python 3.0 was released on December 2008. It was designed to rectify fundamental design flaws in the language. The changes required break backwards compatibility with the 2.x series, which necessitated a new major version number.
print()is available as a builtin but is masked by the print statement syntax, which can be disabled by entering
from __future__ import print_functionat the top of the file.
- Removal of the Python 2
inputfunction, and the renaming of the
input. Python 3's
inputfunction behaves like Python 2's
raw_inputfunction, in that the input is always returned as a string rather than being evaluated as an expression.
filter) out of the built-in namespace and into
functools(the rationale being that operations using reduce are expressed more clearly using an accumulation loop).
- Adding support for optional function annotations that can be used for informal type declarations or other purposes.
- Unifying the
unicodetypes, representing text, and introducing a separate immutable
bytestype; and a mostly corresponding mutable
bytearraytype, both of which represent arrays of bytes.
- Removing backward-compatibility features, including old-style classes, string exceptions, and implicit relative imports.
- A change in integer division functionality: in Python 2,
5 / 2is
2; in Python 3,
5 / 2is
2.5. (In both Python 2 (2.2 onwards) and Python 3,
5 // 2is
- map(function, iterable) changes from python2.7 to python3. In python2.7 map returns a list with the results of applying the function over the iterable, in python3 returns a map object, to retrieve the values, we can use list(map(function, iterable)
Subsequent releases in the Python 3.x series have included additional, substantial new features; all ongoing development of the language is done in the 3.x series.
Porting your code to Python 3
For a complete reference and advise, see https://docs.python.org/3/howto/pyporting.html
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
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!
Newest versions of the ECMWF packages and libraries support now both python 2 and 3.
When running on an ECMWF platform, all recent installations of ECMWF software packages and libraries can now be used with python 2.7 and 3.6. In order to work with the new python 3, you should:
You may then run your python scripts by:
or editing the first line of your script, if you run it as an executable:
For sustainability and maintainability reasons, some extra modules available in Python 2 may not be installed in the Python 3. If you need a module that is missing, please consider if you can use it through a virtual environment. This is particularly recommended if the module is being evaluated or tested, or if it's not going to be used widely at the centre. If in doubt, please get in touch using the ECMWF Support Portal at https://support.ecmwf.int