Skip to content

Running Legacy Python 2 Code on Eagle#

What is Legacy Code?#

One definition of "legacy" code or software is code was written in the past using currently outdated, obsolete, or otherwise deprecated, compilers, functions, methods, or methodology.

While Python 2 was sunset on January 1, 2020 in favor of Python 3, there is still "legacy" Python 2 software that may need to be run on Eagle. We always encourage Eagle users to upgrade their code to Python 3.x to continue receiving official updates, bug fixes, and security patches. But we do understand that there will always be code that is not worth porting to Python 3. In those cases here are some options you have

  1. Set up a custom Python 2 environment
  2. Check for updates
  3. Find an alternative tool that fits your needs
  4. Convert Python 2 code to Python 3

1. Set up a custom Python 2 environment using Conda or containers#

It is best to create this python environment within conda. For example,

conda create --name my_environment python=2

This conda environment can be made even more portable by using Docker or Apptainer. Currently, Eagle only supports Apptainer. Subsequently, the outputs of the Python 2 code can be incorporated, e.g., using a file-based approach.

It is possible to run legacy Python 2 code in parallel within a container or a conda environment, but your mileage may vary.

2. Check for updates#

If the software or module is still under active development, it's highly likely that the authors have transitioned the software to Python 3.x. If an updated version is available, you should strongly consider it over an outdated Python 2 version. It will likely be more secure, have better performance, be more reliable, and have a longer shelf life for reproducibility in the future.

3. Find an alternative tool that fits your needs#

There are usually multiple alternatives to your software of concern that have the same or slightly different features. Some of the potential advantages and disadvantages include:

Advantages

  1. If they are written in Python 3, they are newer and might offer better software support.
  2. They allow for efficient parallelism, enabling better use of Eagle.
  3. Leverages Python 3 features and packages, e.g., Abstract Base Classes.

Disadvantages

  1. It may break your build environment.
  2. It may require a significant code rewrite to extract the best performance.
  3. Additional code may need to be written if the alternative does not have all the features as the Python 2 software.

4. Convert Python 2 code to Python 3#

This is an option when the Python 2 code under consideration does not have a lot of dependencies, you have the source code, and the software license allows you to make changes to the source code. For scientific software, we do encourage developers to make this version jump as this enables code longevitiy and accessibility. Several online resource are available to enable porting your code to Python 3. Some of them include:

  1. Porting Python 2 Code to Python 3
  2. The Conservative Python 3 Porting Guide
  3. Supporting Python 3: An in-depth guide
  4. Python FAQ: How do I port to Python 3?
  5. How to Port Python 2 Code to Python 3
  6. 2to3 — Automated Python 2 to 3 code translation

Note that the above is not an exhasutive list and we encourage the user to look at other resources as well. Additionally, depending on the needs of your project, you can also reach out to HPC-Help@nrel.gov to help with this port.

Remember, since Python 2 has been officially deprecated, more and more code is either being updated or rewritten entirely in Python 3 as time passes. Additionally, the community-maintained Python 2 packages in the default conda channels will likely disappear at some point in the future. Keeping your code modernized to the latest standards will help ensure both the longevity and reproducibility of your software and your results.