Installing and Managing Development ToolsLesson 4.2
How to install Python and manage versions with pyenv in WSL2
system Python risks, pyenv installation, pyenv install, pyenv global, pyenv local, .python-version file, pip usage, virtual environments intro
Python with pyenv
WSL2 ships with Python 3, but modifying the system Python risks breaking apt and OS tools. pyenv lets you install and switch Python versions per project without touching the system installation.
Install pyenv dependencies and pyenv
sudo apt install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl
curl https://pyenv.run | bashAdd to ~/.bashrc:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"Install and set a Python version
source ~/.bashrc
pyenv install 3.12.3
pyenv global 3.12.3
python --versionSet a version per project
cd ~/projects/my-api
pyenv local 3.11.8
# Creates .python-version — pyenv activates it automaticallyUse pip and python normally after setting the version. Always create virtual environments (python -m venv .venv) before installing packages in a project.
