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.
Install
The simplest way to install pyenv is by using the automatic pyenv-installer.
Open a terminal and run the installer:
Then, as the installation message suggests, append the following to your shell init script (typically ~/.bash_profile, ~/.profile, ~/.bashrc or ~/.zshrc).
export PYENV_ROOT="$HOME/.pyenv"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Close the current terminal and open a new one. Run the following to validate a successful installation:
List Installable Versions
The following command lists the Python versions that are available for installation.
Install a Python Version
The following command installs a given Python version from the list of installable versions.
List Usable Versions
The following command lists the Python versions that are installed on your system and available for use. The * denotes the active version.
Change the Current Shell Python Version
The following command configures a given Python version for the current shell session. It doesn't modify the system's default. Opening a new shell will return the the global version.
Change the Global Python Version
The following command configures an installed version as the system's default Python version.
Create/Delete a Virtual Environment
The following commands create and delete a virtual environment from a given Python version. Virtual environments are a way of getting a clean, isolated Python installation.
Activate/Deactivate a Python Environment
The following commands activate and deactivate a given Python environment.
Configure an Environment for a Project
The following command configures a directory to automatically activate a virtual environment whenever you navigate into it. This is my absolute favorite trick.
To remove the configuration, simply remove the .python-version file.
Comments