Jump to content

Python

From Sinfronteras
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Install and manage multiple Python versions with pyenv

Es lo que usan la mayoría de devs profesionales hoy.

En Ubuntu 24.04, la forma más limpia, flexible y usada por la mayoría de desarrolladores para instalar varias versiones de Python en paralelo es pyenv. Permite:

  • Tener muchas versiones de Python instaladas a la vez
  • Cambiar de versión por proyecto
  • No romper el Python del sistema (muy importante en Ubuntu)


After installing pyenv, we can manage multiple python versions this way:

# 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

# List available versions 
pyenv install --list

# Instalar python
# 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
pyenv install 3.10.14
pyenv install 3.11.14
pyenv uninstall 3.10.14

# List versiones instaladas
pyenv versions

# Elegir versión global
pyenv global 3.11.14

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

# Verificar
python --version
which python



Creating the venv

pyenv local 3.11.14  # This create he .python-version file in the directory
python -m venv .venv
source .venv/bin/activate
pip install --upgrade pip 

which python
which pip

pip install -r requirements.txt
pip list