Python

Python is Article description::a cross-platform interpreted programming language. Python powers [[Portage, eselect, equery, and many other tools in Gentoo.]]

Installation
Since Python is so integral to Gentoo there is little chance it is not installed. Doing so would be like removing the heart from Gentoo. There is occasion where Python must be recompiled in order to add new features or to upgrade.

Emerge
Python is slotted, which means there is more than one version Python that can be installed on a Gentoo system at the same time. Select the version to install by using an  (colon) and the package atom followed by one of the slot numbers:

For example, to install Python 3.4:

Setting the active interpreter
The active Python interpreter can be set using the command. First list the available interpreters using :

Select the desired interpreter using. In this example the second option was chosen:

Version upgrade
Version upgrades can be elective, or may have to happen due to a profile update.

Profile update
When a Python version becomes deprecated, or is no longer supported, it may cause a profile update to set the PYTHON_SINGLE_TARGET to a higher version. This may cause emerge conflicts when updating a system.

The easiest way to handle this is to add the new Python version to the PYTHON_TARGETS, e.g. for the transition from Python 3.7 to Python3.8:

Then update the system:

Remove the previous Python version, by removing the line added to and rebuild the system again.

Elective version upgrade
Users may select to upgrade their Python version also by choice. Before starting ensure that the system is up to date:

To make system packages be rebuilt using the new Python implementation update the PYTHON_TARGETS and PYTHON_SINGLE_TARGET variables in. For example, when updating from Python 3.6 to 3.7:

Then update the system again using the  options:

You may be asked to update any remaining  USE flags as well.

Note that some popular packages still require PYTHON_SINGLE_TARGET variable to be set to  due to various dependencies.

Cleanup
Once the upgrade has completed, old Python implementations can be removed from the system using:

After depclean will still show old Python versions if they were previously selected, to remove these entries run the following command:

Invocation
A specific version of the Python interpreter can be invoked from the command-line directly by running:

Or

To execute a script with Python 2.x use the following header:

External resources

 * Gentoo Python Guide, a new doc (in 2020) by Michał Górny, a Gentoo developer.
 * News item - Python 3.7 to become the default target