4. PyEnv

https://github.com/pyenv/pyenv

4.1. Install

Install using brew:

$ brew install pyenv

Add the following code to your shell startup file (for zsh, it is .zshrc):

if command -v pyenv > /dev/null; then
    eval "$(pyenv init -)"
fi
$ curl -fsSL https://pyenv.run | bash
Cloning into '/home/ubuntu/.pyenv'...

WARNING: seems you still have not added 'pyenv' to the load path.

# Load pyenv automatically by appending
# the following to 
# ~/.bash_profile if it exists, otherwise ~/.profile (for login shells)
# and ~/.bashrc (for interactive shells) :

export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"

# Restart your shell for the changes to take effect.

# Load pyenv-virtualenv automatically by adding
# the following to ~/.bashrc:

eval "$(pyenv virtualenv-init -)"

Add the scripts mentioned above to .bashrc (Generally, .profile would call .bashrc on Ubuntu).

Show the version:

$ pyenv --version 
pyenv 2.4.11

4.2. Usage

Install a new version:

$ pyenv install 3.12.5
python-build: use openssl@3 from homebrew
python-build: use readline from homebrew
Downloading Python-3.12.5.tar.xz...
-> https://www.python.org/ftp/python/3.12.5/Python-3.12.5.tar.xz
Installing Python-3.12.5...
python-build: use tcl-tk from homebrew
python-build: use readline from homebrew
Installed Python-3.12.5 to /Users/xxxx/.pyenv/versions/3.12.5

Tip

For PyEnv install python by building from sources, so some other dependencies may have to be installed first. See Install Python Development Environment.

List all installable versions:

$ pyenv install --list

Show all vresions:

$ pyenv versions
* system (set by /Users/xxxx/.pyenv/version)
  3.12.5

Set version globally:

$ pyenv global 3.12.5
$ pyenv versions
  system
* 3.12.5 (set by /Users/xxxx/.pyenv/version)

Note the “set by” things.

Set version in current directory:

$ pyenv local 3.12.5
$ pyenv versions
  system
* 3.12.5 (set by /Users/xxxx/.python-version)

Set version in current shell:

$ pyenv shell 3.12.5
$ pyenv versions
  system
* 3.12.5 (set by PYENV_VERSION environment variable)

The decision of effective version is made by the importantce sequence: shell > local > global.