What is pyenv?
Overview
Pyenv is a simple tool to manage multiple versions of Python. This tool helps in the following ways according to the official pyenv GitHub README documentation:
- It sets the global Python version on a per-user basis.
- It provides support for per-project Python versions.
- It allows a user to override the Python version with an environment variable.
Installation
Officially, pyenv does not support Windows operating system. For Windows, we can use pyenv-win as an alternative.
In MacOS, pyenv can be installed and configured in two steps:
-
Install
pyenvusingbrew:brew update brew install pyenv -
Update the
PATHenvironment variable:echo 'PATH=$(pyenv root)/shims:$PATH' >> ~/.zshrc
Syntax
The basic syntax of pyenv is as follows:
pyenv <command> [<args>]
There are several commands associated with pyenv. Let’s take a look at the following commands:
rootinstalluninstallversionlocalglobalprefix
The root command
The root command displays the root directory where versions and shims of pyenv are installed.
The syntax is as follows:
pyenv root
The install command
The install command installs the specified version of Python.
The syntax is as follows:
pyenv install [options|python-version]
In the above, python-version is the version of Python to be installed. For example,
pyenv install 3.9.4
There are various options that can be used with this command. Some of the important ones are as follows:
--list: This option lists the various Python versions available for installation.--force: This option force installs Python even if the version appears to be installed already.--version: This option shows the current Python version.
The uninstall command
The uninstall command deletes the specified version of Python.
The syntax is as follows:
pyenv uninstall [-f|--force] <python-version>
In the above,
python-version: This is the version of Python to be uninstalled. For example,pyenv uninstall 3.9.4--force: This is used to remove the specified version without asking for confirmation.
The version command
The version command shows the current Python versions (or version, if one) and their origin.
The syntax is as follows:
pyenv version
The local command
The local command can be used to either set the local (application-specific) Python version or show the local (application-specific) Python version.
The syntax is as follows:
pyenv local <python-version>
In the above, python-version is the version of the python to be installed locally. For example,
pyenv local 3.9.4
The global command
The global command can be used to either set or show the global Python version.
The syntax is as follows:
pyenv global <python-version>
In the above, python-version is the version of Python to be installed globally. For example,
pyenv global 3.9.4
The prefix command
The prefix command is used to get the directory of a particular Python version.
The syntax is as follows:
pyenv prefix
Playground
In the terminal below, let’s look at some of the above commands in action.
The
pyenv install 3.9.4command will take 3–5 minutes to execute so be patient.
Free Resources