Command Reference (Install & Setup instructions below):
# List all versions of python interpreters available to install:
pyenv install -l
# List all versions of CPython (the default interpreter) available to install:
pyenv install -l | grep -E '^\s*[0-9]'
# Install specified version. In this case it installs 3.12.6
pyenv install 3.12.6
# List versions installed locally:
pyenv versions
# See global version
pyenv global
# Set global version to x.x.x; in this case it sets to 3.12.6
pyenv global 3.12.6
# See local version
pyenv local
# Set global version to x.x.x; in this case it sets to 2.7.18
pyenv global 3.12.6
Intro to PyEnv
Management of different language versions and interpreters is somewhat of a gray area in the Pythonic world as of September 2024, but of course the community has "unofficial" solutions that circumvent this problem.
One particular good one is PyEnv, that borrows heavily from ruby's solutions for version management, which is something ruby does very well.
PyEnv is basically a collection of shell scripts that help to install and select a specific python version or interpreter, in both global and localized scopes.
It works by inserting a directory of shims
in the PATH
and will rehash the shim according to specific conditions in order to map the python
binary (as well as other setup such as mapping pip
, switching the PYTHON_VERSION
env, etc).
This guide will focus on MacOS and the Debian-family linux distros, and will cover setup on both bash
and zsh
. It will not cover pyenv-win.
Installing PyEnv
MacOS
This assumes that homebrew and the xcode-tools are installed.
First line will install dependencies to install and build python versions.
Second line will install pyenv itself.
Third line is optional and will append a pyenv
load before running brew
in order to avoid a false positive warning when running brew doctor
and to avoid errors in case you are developing brew
python packages. Note that it is being placed in .zshrc
, your mac should already have zsh enabled by default unless it's an older machine, in which case you should probably append it to .bash_profile
.
brew install openssl readline sqlite3 xz zlib tcl-tk && \
brew install pyenv \
&& echo "alias brew='env PATH=\"${PATH//$(pyenv root)\/shims:/}\" brew'" >> ~/.zshrc
Linux (Debian-based)
First, install the dependencies.
Then, clone the public repository into $HOME/.pyenv
.
sudo apt update && sudo apt install build-essential \
libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl git \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev && \
git clone https://github.com/pyenv/pyenv.git $HOME/.pyenv
Post Install (Both MacOS & Linux)
Add this to your relevant shell config file, .bash_profile
if you're using bash, .zshrc
if you're using zsh. Find out which with echo $SHELL
if you're unsure.
## pyenv configs
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
Finally, refresh your terminal environment to see changes with exec "$SHELL"
.
After this setup, your python
(not python3
!) will be mapped to your python version set up with pyenv
.
I'd recommend you to then install a python version (see command reference above) and set it up as your global version.
Check that everything worked with python --version
to see if your installed version matches the one you've chosen. If it does not, it likely is still pointing to the system default, so you might have slipped in one of the steps below (likely the part to refresh the terminal, close and reopen your terminal window if you're unsure of what to do).
Have fun with a saner python version management!
Top comments (0)