Install pyenv
Using Homebrew:
brew install pyenv
Add to shell:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init --path)"' >> ~/.zshrc
source ~/.zshrc
Install multiple Python versions
pyenv install 3.10.16
pyenv install 3.11.13
pyenv install 3.12.11
List installed versions:
pyenv versions
Set Python version
Global:
pyenv global 3.12.11
Per project:
cd my-project
pyenv local 3.11.13
This creates:
.python-version
Create venv with specific Python version
python -m venv .venv
or
python3 -m venv .venv
Because pyenv already selected the Python version, the venv uses that version automatically.
Activate:
source .venv/bin/activate
Verify
python --version
which python
Windows
Best options:
- pyenv-win
- Official Python installer with
py launcher
Example:
py -3.10
py -3.11
Create venv with exact version:
py -3.11 -m venv .venv
Activate:
.venv\Scripts\activate
Recommended real-world setup
~/projects/
app-a/
.python-version -> 3.10
.venv/
app-b/
.python-version -> 3.12
.venv/