DEV Community

Cover image for Simple Python Version Management: pyenv
Rusydy
Rusydy

Posted on • Updated on

Simple Python Version Management: pyenv

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

Installation

In this topic, we will install pyenv on macOS.

Install pyenv

pyenv builds Python from source, which means you’ll need build dependencies to actually use pyenv. On macOS, you can install these dependencies with Homebrew:

brew install openssl readline sqlite3 xz zlib
Enter fullscreen mode Exit fullscreen mode

After you’ve installed the build dependencies, you’re ready to install pyenv itself. I recommend using the pyenv-installer project:

curl https://pyenv.run | bash
Enter fullscreen mode Exit fullscreen mode

This will install pyenv along with a few plugins that are useful:

  1. pyenv: The actual pyenv application
  2. pyenv-virtualenv: Plugin for pyenv and virtual environments
  3. pyenv-update: Plugin for updating pyenv
  4. pyenv-doctor: Plugin to verify that pyenv and build dependencies are installed
  5. pyenv-which-ext: Plugin to automatically lookup system commands

Then add the following to your ~/.zshrc:

# Python Version Manager
export PATH="$HOME/.pyenv/bin:$PATH"
alias brew='env PATH="${PATH//$(pyenv root)\/shims:/}" brew'

export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
Enter fullscreen mode Exit fullscreen mode

Usage

List available Python versions

pyenv install --list
Enter fullscreen mode Exit fullscreen mode

Install Python

pyenv install 3.11.0
Enter fullscreen mode Exit fullscreen mode

Set Python version globally

pyenv global 3.11.0
Enter fullscreen mode Exit fullscreen mode

Set Python version locally

pyenv local 3.11.0
Enter fullscreen mode Exit fullscreen mode

Uninstall Python

pyenv uninstall 3.11.0
Enter fullscreen mode Exit fullscreen mode

List installed Python versions

pyenv versions
Enter fullscreen mode Exit fullscreen mode

Update pyenv

brew upgrade pyenv
Enter fullscreen mode Exit fullscreen mode

PIP

Install PIP

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
Enter fullscreen mode Exit fullscreen mode

References

Oldest comments (0)