Pyenv is a Python version control management package that facilitates the seamless installation and switching of Python versions. It can be employed in tandem with virtual environments, such as ‘env,’ to enhance the flexibility of Python project development and management.
Install ‘pyenv’
- Firstly, update the packages and install the necessary dependencies. The following example is demonstrated on CentOS 9. It’s important to note that other operating systems or versions may have distinct names for dependency packages, so please verify before proceeding with the installation.
yum update
yum install gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel tk-devel libffi-devel xz-devel
2. Download pyenv from GitHub
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
3. Add the relevant paths and pyenv init to ~/.bashrc
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
4. Relaunch the shell to apply the updated settings
exec "$SHELL"
5. Run the following command to verify the successful installation
pyenv versions
If you encounter the following message, it indicates a successful installation. Here, ‘system’ represents the original Python version of the system.
Guidance on Installing Various Versions of Python
- Initially, examine the available versions for installation.
pyenv install -l
It will display a list of available versions for you to choose from.
2. Choose and install a specific Python version, such as version 3.10.12
pyenv install -v 3.10.12
3. Display a list of the installed Python versions
pyenv versions
- Upon successful installation, a list of previously installed versions will be displayed.
- The asterisk at the beginning indicates the currently active version. As we have recently installed version 3.10.12 and have not switched yet, it remains set to ‘system’.
4. Now, proceed to switch the Python version
There are three modes available: global, local, or shell.
pyenv shell 3.10.12
pyenv local 3.10.12
pyenv global 3.10.12
In Pyenv, the commands ‘global,’ ‘local,’ and ‘shell’ can all be utilized to switch Python versions, but they have different scopes of effect:
pyenv global [version]
:
Sets the global Python version, effective in all shells for the current user.
pyenv local [version]
:
Sets the Python version in the current directory, effective only for the current directory and its subdirectories.
pyenv shell [version]
:
Switches the Python version in the current shell, effective only for the current shell session. In other words, this change is temporary, and the command will become invalid when the shell exits.The priority order is shell > local > global.
The Python version set withpyenv shell
will take precedence over the version set withpyenv local
, and the version set withpyenv local
will, in turn, take precedence over the version set withpyenv global
.When using
pyenv shell
andpyenv local
orpyenv global
in the same shell, the version specified bypyenv shell
will be prioritized. Similarly, when usingpyenv global
in a directory wherepyenv local
is configured, the version specified bypyenv local
will take precedence.
5. Verify the switched version
Check the current Python version.
python -V
# or python --version
# or python3 --version
# or pyenv versions
As depicted in the figure below, the selected version will be displayed if the switch is successful.
To go back to the default version, execute the command below. The system’s original default Python version is represented by’system’.
pyenv global system
# or pyenv local system
# or pyenv shell system
Keep in mind that when reverting to the default version, the priority order of shell > local > global still applies.
Uninstall
- Uninstall Python versions
If you’re working with Python and want to tidy up your space by removing extra versions, just type pyenv uninstall <versions>
in the command line. This will neatly remove the specified versions, giving you a cleaner and more efficient development setup.
pyenv uninstall 3.10.12
2. Uninstalling pyenv
To fully uninstall Pyenv, start by removing all Pyenv configurations from the ~/.bashrc file. After that, execute the following command to delete its root directory.
rm -rf $(pyenv root)
This will delete all installed Python versions in that directory.