Skip to content

python_2_3_transition

Rudolf Weeber edited this page Jul 5, 2019 · 2 revisions

Dropping support for Python 2

As you may now, Python 2 will not be supported past the end of the year. Also, major Linux distributions will no longer ship it by default in their upcoming releases. Python 3 was released in 2008. Espresso 4.0 can be used with both, Python 2 and Python 3. However, as most other Python-based projects, we are dropping support for Python 2 for our next release (4.1) and in the development branch.

Making sure, you are Python 3 ready

Presence of dependencies to build Espresso

Current Linux distros such as Ubuntu 18.04 LTS already support Python 3. All of Espresso's dependencies can be installed from the Distros package manager, e.g., python3-numpy. We have updated the installation instructions for Ubuntu and Mac: http://espressomd.org/html/doc/installation.html

If you are using Espresso on an HPC cluster, please check if a Python 3 module with the necessary packages such as NumPy is available. If such a module is not available, please ask the cluster operators to install it. On many clusters, the command "module avail" provides a list of loadable modules.

Converting simulation scripts

There are some syntax differences between Python 2 and Python 3, but most simulation scripts can be converted in less than five minutes. The most relevant ones for simulation scripts are:

  • print is now a function
      x = 5
      print("x is", 5)
    
  • Division of two integers returns a float. 5/2 equals 2.5 rather than 2 as in Python 2 and C. To get integer division, use 5//2
  • range() in Python 2 returns a list of items, whereas it returns an iterator in Python 3. For-loops work as before, but if you need the actual list, use
      x = list(range(5))
    

The official documentation is at https://docs.python.org/3/howto/pyporting.html There are also tools for automatic conversion: https://docs.python.org/2/library/2to3.html