Managing Multiple Python Versions with Pyenv: Installation and Configuration Guide

David Childs September 25th, 2024 Categories: Tutorial

Python version management is critical in modern development environments. Pyenv, a popular tool for this purpose, allows developers to seamlessly switch between Python versions. However, discrepancies can arise between the configured pyenv version and the system's active Python installation.

This technical guide addresses a specific issue: when pyenv global is set to version 3.10.10, but python3 --version reports an older version. The root cause often lies in shell configuration, PATH variable ordering, or shim directory precedence.

  1. install pyenv : brew install pyenv official installation guide
  2. install particular python: pyenv install 3.10.10
  3. set python3.10.10 to gloabal python env: pyenv global 3.10.10

If you have already executed pyenv global 3.10.10, but python3 --version still shows an older version (such as 3.9), it might be because pyenv is not correctly configured in your shell environment, or the system's PATH environment variable is not properly updated. Here are some steps to resolve this issue:

1. Ensure pyenv is Properly Installed and Initialized

First, make sure you have correctly installed pyenv, and that the initialization script for pyenv has been added to your shell configuration file.

For zsh Users:

Edit the ~/.zshrc file:

nano ~/.zshrc

Add the following lines (if not already added):

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

For bash Users:

Edit the ~/.bashrc or ~/.bash_profile file:

nano ~/.bashrc

Add the following lines (if not already added):

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

2. Reload the Shell Configuration File

Save the file and exit the editor, then reload the configuration file:

For zsh Users:

source ~/.zshrc

For bash Users:

source ~/.bashrc

3. Confirm pyenv Configuration

Run the following command to ensure pyenv is correctly configured:

pyenv versions

You should see output similar to the following, where 3.10.10 has an asterisk indicating it is the global version:

* 3.10.10 (set by /Users/hu/.pyenv/version)
  system

4. Check the PATH Environment Variable

Ensure that the pyenv shims directory is at the front of the PATH environment variable. Run the following command to check the PATH:

echo $PATH

Ensure the output includes a path like ~/.pyenv/shims, and that it appears before other Python paths.

5. Verify the python3 Version

Finally, verify the python3 version:

python3 --version

You should see the version Python 3.10.10.

6. Additional Checks

If the above steps still do not resolve the issue, you can try the following command to ensure the pyenv shims are up to date:

pyenv rehash

Also, ensure there are no other Python installation paths interfering:

which python3

It should point to ~/.pyenv/shims/python3.

By following these steps, you should be able to successfully set pyenv to use version 3.10.10 as the default python3.

source: https://stackoverflow.com/a/78502700