Python
Python is 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.
USE flags
USE flags for dev-lang/python An interpreted, interactive, object-oriented programming language
+ensurepip
|
Install the ensurepip module that uses bundled wheels to bootstrap pip and setuptools (if disabled, it will be only possible to use venv `--without-pip`) |
+ncurses
|
Add ncurses support (console display library) |
+readline
|
Enable support for libreadline, a GNU line-editing library that almost everyone wants |
+sqlite
|
Add support for sqlite - embedded sql database |
+ssl
|
Add support for SSL/TLS connections (Secure Socket Layer / Transport Layer Security) |
+xml
|
Add support for XML files |
berkdb
|
Add support for sys-libs/db (Berkeley DB) |
bluetooth
|
Build Bluetooth protocol support in socket module |
build
|
!!internal use only!! DO NOT SET THIS FLAG YOURSELF!, used for creating build images and the first half of bootstrapping [make stage1] |
debug
|
Enable extra debug codepaths, like asserts and extra output. If you want to get meaningful backtraces see https://wiki.gentoo.org/wiki/Project:Quality_Assurance/Backtraces |
examples
|
Install examples, usually source code |
gdbm
|
Add support for sys-libs/gdbm (GNU database libraries) |
jit
|
Enable experimental Just-In-Time compilation support. |
libedit
|
Use the libedit library (replacement for readline) |
pgo
|
Optimize the build using Profile Guided Optimization (PGO) by running Python's test suite and collecting statistics based on its performance. This will take longer to build. |
tail-call-interp
|
Enable the tail call interpreter. May lead to better performance but is still new and dependent on latest toolchain versions (Clang 19, GCC 15). |
test
|
Enable dependencies and/or preparations necessary to run tests (usually controlled by FEATURES=test but can be toggled independently) |
tk
|
Add support for Tk GUI toolkit |
valgrind
|
Disable pymalloc when running under dev-debug/valgrind is detected (may incur minor performance penalty even when valgrind is not used) |
verify-sig
|
Verify upstream signatures on distfiles |
wininst
|
Install Windows executables required to create an executable installer for MS Windows |
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.8:
root #emerge --ask dev-lang/python:3.8Configuration
Setting the active interpreter
The default (non-versioned) active interpreter is controlled through package dev-lang/python-exec. See python-exec wiki
Each time dev-lang/python-exec is emerged/rebuilt, use dispatch-conf, etc-update or similar to accept or deny proposed modification to /etc/python-exec/python-exec.conf (that control the default Python interpreter)
Python preference to follow PYTHON_TARGETS official statement - 2021-01-30
The older way to change to the active Python (non-versioned) interpreter using the eselect python command will remain as a wrapper to python-exec.conf modification if app-eselect/eselect-python is in the @world set. See eselect-python wiki
Version upgrade
Python 2.7 has reached its end-of-life by January 1, 2020, as per this official statement.
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 python3_7 to python3_8:
/etc/portage/package.use*/* PYTHON_TARGETS: python3_7 python3_8
Then update the system:
root #emerge --ask --update --newuse --deep @worldRemove the support for the previous Python version, by removing the line added to /etc/portage/package.use 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:
root #emerge --ask --update --newuse --deep @worldTo make system packages be rebuilt using the new Python implementation update the PYTHON_TARGETS and PYTHON_SINGLE_TARGET variables in /etc/portage/package.use. For example, when updating from Python 3.6 to 3.7:
/etc/portage/package.use*/* PYTHON_TARGETS: python3_7
*/* PYTHON_SINGLE_TARGET: -* python3_7
Then update the system again using the --changed-use --deep options:
root #emerge --ask --changed-use --deep @worldYou may be asked to update any remaining python_targets_python3_6 USE flags as well.
Note that some popular packages still require PYTHON_SINGLE_TARGET variable to be set to python2_7 due to various dependencies.
Cleanup
Once the upgrade has completed, old Python implementations can be removed from the system using:
root #emerge --ask --depclean dev-lang/pythonAfter depclean eselect python list will still show old Python versions if they were previously selected, to remove these entries run the following command:
root #eselect python cleanupUsage
Invocation
A specific version of the Python interpreter can be invoked from the command-line directly by running:
user $python2.7Or
user $python3.7To execute a script with Python 2.x use the following header:
/usr/local/bin/repo#!/usr/bin/env python2
...
See also
- Bash — the default shell on Gentoo systems and a popular shell program found on many Linux systems.
- Pip — Python's package management system. It references packages available in the official Python Package Index (PyPI).
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
- Python preference to follow PYTHON_TARGETS