Managing Python Environments with pyenv

pyenv is a tool for managing multiple Python environments. It allows you to easily install and switch between different Python versions.

Installing pyenv
git clone https://github.com/pyenv/pyenv.git ~/.pyenv

# Install with brew
brew install pyenv

# Auto installer
curl https://pyenv.run | bash
# Adding pyenv to the shell
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
source ~/.bashrc
List and install a specific version
# show installed versions:
pyenv versions

# list the available versions of python3
pyenv install --list | grep " 3\.[6789]"

# Install a specific python version
pyenv install 3.9
# Switch to the version globally
pyenv global 3.9
# Switch to the version locally
pyenv local 3.9
# Switch to the version per shell
pyenv shell 3.9
# Unset version
pyenv local --unset
pyenv shell --unset
Creating a virtual environment
pyenv virtualenv 3.9 project_environment

# Activating the virtual environment
pyenv activate project_environment

# Deactivating the virtual environment
pyenv deactivate