Migrate Python 2 to Python 3

 

The Great Lakes, Armis2, and Lighthouse computing clusters are transitioning away from Python 2. With the upgrade of the computing clusters to Red Hat 8.4 during the Summer 2022 Maintenance, Python 2 is no longer provided via the python command on these clusters as it was prior to the upgrade, with the following ramifications:

  • Executing the command python will produce a diagnostic message.
  • To execute Python 2, the command python2 must be used.
  • To execute Python 3, the command python3 must be used.

The set of options below allows for the continued use of Python 2 in your scripts in the near term. However, please note that when Red Hat’s support for security updates for Python 2 finally ends, currently slated for June 2024, it will be removed from the computing clusters and ARC support for Python 2 will cease. ARC strongly recommends that you migrate your code to Python 3.

Two key points are motivating this transition:

    1. The Python developers have ceased supporting Python 2.7 as of 2020.
    2. The RHEL 8 AppStream Lifecycle Page puts the end date of RHEL 8’s Python 2.7 package at June 2024.

Recommendation: Migrate your code from Python 2 to Python 3

ARC recommends migrating your code from Python 2 to Python 3. The following links provide further information and some suggested tools that you may find helpful:

Please be aware that automated tools may produce code which has inaccurate results or is not reliable in all cases.

Alternatives: if migration is not an option

If the code you run cannot be migrated to Python 3, the following are a list of alternatives to migration. These will allow continued use of the Python 2 interpreter on the computing clusters. After June 2024, the container alternative may be the only viable one.

1. Create a link to python2

By creating a link in your own bin directory (or any directory in your PATH where you have write access) you can continue to invoke python2 as python. For example, if your ~/bin directory is in your PATH, the following command will set up this link:

$ ln -s /usr/bin/python2 ~/bin/python
$ hash python

The second statement is needed if you use the bash shell and you want it to notice the change right away, alternatively you can log out and back in.

Please note that this link must be placed in a directory that precedes /usr/bin in your PATH, as is usually the case.

2. Alter the first line of your Python scripts

By changing the first line of your Python scripts, you can invoke python2 explicitly. For example, if the first line of a script reads

#!/usr/bin/python

then changing this line to

#!/usr/bin/python2

will allow your script to run using the Python 2 interpreter.

3. Create a container

By placing your application in a container, you can fully customize its execution environment, including the direct use of Python 2 in any scripts. This container should include all the necessary Python 2 runtime and packages, and this may be the only solution that will work after June 2024.  

 

Need help? Contact the ARC Help Desk, arc-support@umich.edu or visit CoderSpaces, virtual, drop-in office hours.