Using Python Version Manager pyenv
pyenv is a lightweight, command-line tool that lets you easily install, manage, and switch between multiple versions of Python on your system—without interfering with the system Python. It's ideal for developers who need to test code across different Python versions or maintain isolated environments for different projects. pyenv supports per-project version settings, global defaults, and integrates with plugins like pyenv-virtualenv to manage virtual environments seamlessly.
✅ Why use pyenv?
- Manage multiple Python versions cleanly without affecting system Python
- Project-specific Python versions via
.python-versionfiles - Integrated plugin ecosystem (e.g.,
pyenv-virtualenv) for virtual env workflows
1. Install pyenv
- Ensure OS build dependencies are installed (e.g.,
build-essential,libssl-dev,zlib-dev, etc.)
Add to shell init (~/.bashrc, ~/.zshrc):
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
Then reload your shell
Install via installer:
curl https://pyenv.run | bash
This includes pyenv, plus plugins like pyenv-virtualenv and pyenv-update
2. Install Python Version(s)
Install a specific version:
pyenv install 3.9.1
(Builds from source, may take a while)
List available versions:
pyenv install --list
3. Set Active Python Version
| Scope | Command | Effect |
|---|---|---|
| Global | pyenv global 3.9.1 |
Sets default Python for your user |
| Local | pyenv local 3.9.1 |
Creates .python-version in current directory |
| Shell | pyenv shell 3.9.1 |
Sets Python for the current shell session |
Verify current version:
pyenv versions
python -V
which python
4. Create & Manage Virtual Environments (with pyenv-virtualenv)
Activate/deactivate manually:
pyenv activate myenv
pyenv deactivate
``` :contentReference[oaicite:25]{index=25}
Activate via directory:
pyenv local myenv
(automatically switches when entering the directory) (realpython.com)
Create a new virtualenv:
pyenv virtualenv 3.9.1 myenv
``` :contentReference[oaicite:20]{index=20}
5. Tips & Help Commands
Update pyenv definitions:
pyenv update
``` :contentReference[oaicite:31]{index=31}
Uninstall a version:
pyenv uninstall 3.9.1
``` :contentReference[oaicite:29]{index=29}
List all pyenv commands:
pyenv commands
``` :contentReference[oaicite:27]{index=27}