Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 5Python 3 · Languages and build

Install and set up Python 3 (pip and venv)

Python is the main language for DevOps scripts, automation and tools: Ansible, the AWS and Azure SDKs, many security scanners and most API scripts are Python. This guide installs Python 3 with pip and venv on RHEL and Ubuntu, sets up the clean way to work (one virtual environment per project, pipx for tools), and explains the errors every beginner meets, like externally-managed-environment.

  • 9 Lessen
  • 8 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

5.1

Know it before you install

The system has its own Python that the OS tools need. Never change its packages. Make a virtual environment (venv) for each project: a private folder with its own Python packages.

ItemValue
Programspython3, pip (inside a venv), python3 -m venv
RHEL packagespython3, python3-pip; newer versions: python3.12, python3.12-pip
Ubuntu packagespython3, python3-pip, python3-venv, pipx
pip settings~/.config/pip/pip.conf (user), /etc/pip.conf (system)
A project's packagesrequirements.txt (or pyproject.toml)
Networkpypi.org and files.pythonhosted.org (443)

Versions: RHEL 9 has Python 3.9 as python3 plus 3.11 and 3.12 as extra packages; RHEL 10 has 3.12. Ubuntu 24.04 has 3.12.

Commando's in deze les

5.2

Install on RHEL, Rocky, Alma and Fedora

dnf -y install python3 python3-pip
python3 --version
Python 3.9.21
dnf -y install python3.12 python3.12-pip
python3.12 --version
Python 3.12.9

Op het werk

Use a newer version for a project with python3.12 -m venv .venv. Do not change what python3 points to — dnf and other OS tools need the default version.

Commando's in deze les

5.3

Install on Ubuntu and Debian

apt-get update
apt-get install -y python3 python3-pip python3-venv pipx
python3 --version
Python 3.12.3

5.4

First configuration: one venv per project

This is the professional way. Make the venv once, turn it on, and install packages only inside it:

mkdir ~/api-tool && cd ~/api-tool
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install requests pyyaml
pip freeze > requirements.txt
deactivate

On another machine (or in CI), make the same environment from the file:

python3 -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt

Settings to make first:

  • Add .venv/ to .gitignore — the venv is never committed, only requirements.txt.
  • Pin versions in requirements.txt (requests==2.32.3), so every build is the same.
  • Command-line tools (not libraries) go in pipx: every tool gets its own venv, and the command is on your PATH.
pipx ensurepath
pipx install ansible-core
pipx install pre-commit
pipx list

A company package mirror or proxy is set in pip.conf:

~/.config/pip/pip.conf
[global]
index-url = https://nexus.example.com/repository/pypi/simple
timeout = 30

5.5

Daily use: the commands and the real uses

source .venv/bin/activate
pip install -r requirements.txt
pip list --outdated
pip show requests
python script.py --help
python -m json.tool data.json
python -m http.server 8000 --bind 127.0.0.1
python -c 'import ssl; print(ssl.OPENSSL_VERSION)'

Real uses at work:

  • Scripts that call APIs (cloud, Jira, GitHub) with requests or boto3.
  • Automation: Ansible modules and custom tools; log parsing; reports to CSV and JSON.
  • Small web services and health checks; glue code in CI pipelines.

5.6

Make it safe

Security checklist:

  • Never sudo pip install — it can break OS tools. Use a venv or pipx.
  • Pin versions; for strong builds use hashes: pip install --require-hashes -r requirements.txt.
  • Check for known CVEs: pipx run pip-audit -r requirements.txt.
  • Watch the package name: fake packages with near names (typosquatting) steal secrets.
  • Keep secrets in environment variables or a secret manager, not in the script.
  • Do not run scripts as root unless they really need it.

5.7

Update and remove

dnf -y upgrade python3 python3-pip
apt-get install --only-upgrade -y python3 python3-pip python3-venv
pip install --upgrade -r requirements.txt
rm -rf .venv

Let op

Never remove the system python3 package: dnf (RHEL) and many Ubuntu tools need it. Remove only extra versions like python3.12.

5.8

Fix common problems

ProblemCauseFix
error: externally-managed-environmentUbuntu 23.04+ / Debian 12 block pip on the system Python (PEP 668).Use a venv (python3 -m venv .venv) or pipx for tools.
The virtual environment was not created successfully because ensurepip is not availablepython3-venv is missing (Ubuntu).apt-get install -y python3-venv.
pip: command not foundpip is not on the PATH.Use python3 -m pip, or activate the venv.
ModuleNotFoundErrorThe package is in another venv, or the venv is not active.source .venv/bin/activate; which python.
SSL: CERTIFICATE_VERIFY_FAILED behind a company proxyThe proxy's CA is unknown.pip config set global.cert /etc/pki/ca-trust/source/anchors/corp.pem (and REQUESTS_CA_BUNDLE).
Wrong Python versionMany versions installed.Make the venv with the exact one: python3.12 -m venv .venv.

5.9

Next steps

1Why not sudo pip install on a server?

It changes the system Python that the OS tools need, and can break them. Use a venv or pipx.

2What goes into Git: the .venv folder or requirements.txt?

Only requirements.txt. The venv is made again from it on each machine.

Alle commando's in dit hoofdstuk