pyenv

Simple Python version management

Installation

curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

Restart your shell so the path changes take effect.

exec $SHELL

Configuration

Zsh note: Modify your ~/.zshrc file instead of ~/.bashrc.

$ vim ~/.bashrc
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Restart your shell so the path changes take effect.

exec $SHELL

Usage

Create Virtual Environment

pyenv install 3.9.12
pyenv virtualenv 3.9.12 <virtualenv>-3.9.12
pyenv local <virtualenv>-3.9.12
pyenv shell <virtualenv>-3.9.12

Integration with visual studio code

Open the command palette in each project and change the interpreter.

  • Python: Select Interpreter
  • Jupyter: Select Interpreter to start Jupyter server

Open Kernel Options in your Jupyter notebook and change the interpreter.

  • Select kernel

Troubleshooting

Python command not found.

$ vim ~/.bashrc
alias python="$(pyenv which python)"
alias pip="$(pyenv which pip)"

If the virtual environment does not appear in the interpreter, reopen the file or terminal.

Commands

Lists all Python versions known to pyenv.

pyenv versions

To list the all available versions of Python, including Anaconda, Jython, pypy, and stackless.

pyenv install --list

Then install the desired versions.

pyenv install 3.9.12

Sets the global version of Python.

pyenv global 3.9.12
exec $SHELL
python --version

Using pyenv virtualenv with pyenv.

pyenv virtualenv 3.9.12 <virtualenv>-3.9.12

Sets a local application-specific Python version by writing the version name to a .python-version file in the current directory.

pyenv local <virtualenv>-3.9.12

Sets a shell-specific Python version by setting the PYENV_VERSION environment variable in your shell.

pyenv shell <virtualenv>-3.9.12

Activate and deactivate a pyenv virtualenv manually.

pyenv activate <virtualenv>-3.9.12
pyenv deactivate

Uninstall a specific Python version.

pyenv uninstall <virtualenv>-3.9.12

Command Reference

Like git, the pyenv command delegates to subcommands based on its first argument.