2 min read

Managing Multiple Python Versions with Pyenv

pyenv is a tool that lets you easily switch between multiple versions of Python. It is simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.

Windows Users (pyenv-win)

Standard pyenv does not support Windows natively. Windows users should use the fork pyenv-win.

Installation (PowerShell)

Run the following in PowerShell to install:

Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &"./install-pyenv-win.ps1"

Note: You may need to restart your terminal or add environment variables manually if the script fails to do so.

macOS / Linux Users

Installation

curl https://pyenv.run | bash

Follow the instructions output by the script to add pyenv to your shell configuration (e.g., .bashrc or .zshrc).

Basic Usage

1. List Available Versions

To see what versions of Python are available for installation (this list is fetched from python.org):

pyenv install --list

2. Install a Python Version

To install a specific version (e.g., 3.10.11):

pyenv install 3.10.11

3. Set Global Version

To set the default Python version for your entire system (user profile):

pyenv global 3.10.11

4. Set Local Version (Per Project)

To set a specific Python version for a specific project folder. Navigate to your project folder and run:

pyenv local 3.9.7

This creates a .python-version file in the directory. Whenever you enter this directory, pyenv will automatically switch the python command to use version 3.9.7.

Using with Virtual Environments

pyenv manages Python installations. You still use venv to manage dependencies.

  1. Set the local python version: pyenv local 3.11.0
  2. Create the virtual environment: python -m venv .venv

The virtual environment will be created using the Python version currently active via pyenv.

programming/python/python