Python2 and Python3 Compatability

Overview

The Python package “python-future” can be installed to permit Python3 scripts to run under Python2, and/or to convert Python2 scripts to Python3.

Installation

To install python-future to a Python2 build, simply load the necessary Python module (or use the native Python2 installation) and run:

pip install future

Using or Conversion

Experience has shown that simply installing future enables one to open the Python interactive prompt and begin importing Python3 modules.  For example:

python
>>> import _posixsubprocess
>>>

One can also use the executable “pasteurize” to convert Python3 scripts to compatible versions that will run in Python2.  For example:

module load python/2.7.9
pasteurize -w /path/to/my/script.py

For upgrades of simple Python2 scripts, use “futurize”:

module load python/2.7.9
futurize -w /path/to/my/script.py

For the conversion of more complex Python2 projects, one may choose to use the two-stage approach.

More Information

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.