2 Installing Python
This chapter describes how to install several versions of Python and switch between them on the same Oracle Linux system.
To install Python and prerequisites, use the package manager. Several versions of Python can be installed and used simultaneously on the same machine, such as Python 2.7 and Python 3.6. You can identify which versions of Python you have installed as follows:
python2 --version
python3 --version
The python
command isn't aliased by default in Oracle Linux 8. Specify the
Python interpreter version with the python2
and python3
commands when you run Python scripts.
If you need to alias the python
command to fix
compatibility problems with existing scripts and applications, you
can set it manually. For example, to set Python 2 as the default
interpreter version:
sudo alternatives --set python /usr/bin/python2
For more information about using the python
command, see the python(1)
manual page.
Note:
Python 3.6 is supported for the full lifespan of Oracle Linux 8.
Application Stream packages, such as Python 2.7 and more recent versions of Python 3, have their own major version releases and can have shorter support lifespans. For more information, see Oracle Linux: Product Life Cycle Information.
Installing Python 2
Important:
Python 2 is no longer maintained by the Python community. We recommend that you migrate existing Python 2 scripts and applications to Python 3.
To learn about the 2to3
automated migration tool visit https://docs.python.org/3/library/2to3.html. See
also the porting guide at https://portingguide.readthedocs.io/en/latest/ for more in-depth information.
To install Python 2.7 on an Oracle Linux 8 system:
sudo dnf module install python27
Installing Python 3
To install Python 3.6 on an Oracle Linux 8 system:
sudo dnf module install python36
If the system is running Oracle Linux 8.4 or later, you can optionally also install Python 3.9:
sudo dnf module install python39
If the system is running Oracle Linux 8.8 or later, you can optionally also install Python 3.11:
sudo dnf install python3.11
Note:
The application streams for Python 3.11 or later don't need to be installed with the
dnf module
command to prevent conflicts with other Python application
streams.
Several versions of Python 3 can be installed and used simultaneously on the same machine, and you can specify which installation you intend to use as follows:
python3.6 --version
python3.9 --version
python3.11 --version
The python3
command is aliased to Python 3.6, so for newer versions of
Python 3 you need to explicitly reference the correct binary.
Installing Extra Python Libraries
You can also install extra dependencies from the Oracle Linux yum server. For example, to
install the requests
library for the default runtime version of Python 3, you
would install the python3-requests
package:
sudo dnf install python3-requests
To install dependency packages for specific Python versions, add the runtime version to the
package name. For example, to install the requests
library for Python 3.11,
run the following command:
sudo dnf install python3.11-requests
Dependencies that are installed in this way are available for any compatible Python installations on the same system. In addition, any matching packages can also be removed without also removing existing Python installations.