Managing Python Versions with Pyenv

David Childs

Master Python version management with pyenv. Complete installation and configuration guide for seamless multi-version development workflows.

  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

Share this article

DC

David Childs

Consulting Systems Engineer with over 10 years of experience building scalable infrastructure and helping organizations optimize their technology stack.

Related Articles