Jump to content

Python

From Sinfronteras
Revision as of 10:02, 7 November 2026 by Adeloaleman (talk | contribs) (→Installing libraries)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Via APT - Installing Python, pip and venv

sudo apt install -y python3.12
sudo apt install -y python3-pip
sudo apt install -y python3.12-venv

python3.12 -m venv .venv  # Creating the venv



Via pyenv - Install and manage multiple Python versions

pyenv builds Python with pip, venv and ensurepip. ensurepip is a standard Python module whose only job is to make sure pip exists and works for a given Python installation. So, no further installation of python3-pip or python3.11-venv is required.



Installing pyenv

# Install system dependencies
sudo apt install -y build-essential curl git libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

# Install pyenv
curl https://pyenv.run | bash

vi ~/.bashrc
# ------
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"

eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
# ------

source ~/.bashrc

pyenv -v



Managing Python with pyenv

pyenv install --list     # List available versions
pyenv install 3.10.14    # Installing Python
pyenv uninstall 3.10.14
pyenv versions           # List versiones instaladas
pyenv global 3.11.14     # Elegir versión global

# Elegir versión por proyecto. Esto crea un archivo .python-version en la carpeta
cd mi_proyecto
pyenv local 3.11.14

python --version         # Verificar
which python

python -m venv .venv     # Creating the venv



Installing libraries

source .venv/bin/activate
which python
which pip

vi requirements.txt
# ---
fastapi[standard]>=0.112.0,<0.113
SQLAlchemy>=2.0.32,<2.1
passlib>=1.7.4,<1.8
bcrypt>=4.2.0,<4.3
python-dotenv>=1.0.1,<1.1
pydantic>=2.8.2,<2.9
# ---

pip install --upgrade pip
pip install -r requirements.txt
pip list



Via uv

Over 2024–2025, uv became the most common choice, and as far as I know that is still true now.

  • It installs the exact Python version for you. uv reads your existing .python-version (3.12.12) and downloads that version if it's missing, on both your laptop and the server. Today you depend on whatever Python each machine has.
  • A real lockfile. Your requirements.txt pins only the packages you list. Their own dependencies (e.g. starlette, pydantic-core, anyio) aren't pinned, so a fresh install on the server months from now can get different versions than your laptop. uv's uv.lock pins every package, with hashes, so both machines install exactly the same thing.
  • Separate dev dependencies. Your requirements.txt notes that pytest and httpx end up on the production server. uv supports dependency groups: uv sync installs everything locally, and uv sync --no-dev on the server leaves the test tools out.
  • No venv activation and simpler scripts. uv run fastapi dev app/main.py or uv run pytest uses the project's venv automatically. run_dev.py could drop its .venv/bin vs .venv\Scripts logic and the "create the venv first" message, because uv run creates the venv and installs dependencies when needed.
  • Speed. Installs are typically 10–100× faster than pip. That matters little for a project this size, but it's pleasant.