Python 2 reached end of life (EOL). However, there are still many projects that
use Python 2. Staying with Python 2 is certainly no long-term option for most
Porting a larger program to Python 3 brakes backward compatibility with
Python 2. One solution for this problem is the single-source approach that
results in source code that runs with Python 2 and 3 without any changes.
This tutorial provides a short overview over the most important differences
between Python 2 and 3. After looking at different approaches for Python 3
support, the focus will be on writing single-source programs with
python-future. You will learn how to port to Python 3 without loosing Python 2
support. Finally dropping Python 2 support will get as simple as removing a few
The content of this tutorial is inspired by questions I received from
participants in trainings about how to smoothly transition from Python 2 to
Python 3. Making Python 2 working as much as possible as Python 3 seems the best
option to me. You are encouraged to bring your questions about this topic.
I teach about this topic on a regular basis in my trainings. Being a full-time
profession trainer, I have plenty opportunity to get involved with many
programmers who have to deal with this problem.
You will need Python 3.8 installed on your laptop. Python 3.6/3.7 should also
work. You also need Python 2.7 installed to test if the code runs with Python 2
and Python 3. You may use Python 3.9 if is released at the time of the tutorial
and all dependencies can be installed.
I will use a JupyterLab for the tutorial because it makes a very good
teaching tool. You are welcome to use the setup you prefer, i.e editor, IDE,
REPL. If you also like to use a JupyterLab, I recommend
easy installation. Similarly to
conda allows creating isolated
environments but allows binary installs for all platforms.
There are two ways to install
Use Minconda. This is a small install and (after you installed it)
you can use the command
conda to create an environment:
conda create -n pycon2020py38 python=3.8
Now you can change into this environment:
conda activate pycon2020py38. The prompt should change to
Now you can install JupyterLab:
conda install jupyterlab.
Do the same with Python 2.7, i.e.
conda create -n pycon2020py27 python=2.7
and activate accordingly
conda activate pycon2020py27.
Install the dependencies:
Hint: You do all this in one command:
conda create -n pycon2020py38 python=3.8 six jupyterlab future
conda create -n pycon2020py27 python=2.7 six jupyterlab future
You can create a comparable setup with virtual environments and
pip, if you
After creating a new environment, the system might still work with some
stale settings. Even when the command
which tells you that you are using an
executable from your environment, this might actually not be the case.
If you see strange behavior using a command line tool in your environment,
hash -r and try again.