I've been using pyenv on the Mac unwittingly since you're not really supposed to use the Python that ships with the operating system.
So you're forced to install Python from python.org, or as many Mac users who are software developers do, install Homebrew and use that package management system to manage all the software development tools.
Linux and Raspberry Pi
The process is the same.
- Update the system.
- Install required dependencies for pyenv.
- Install pyenv.
- Update some environment variables.
- Reset the shell
- Install a Python version using pyenv.
- Set the installed Python version as the default.
- Start programming!
In addition to that, I'll also show you how to create a Python virtual environment.
Update the system
sudo apt updateYou may need to upgrade your system based on the results of the above command. If so, type the following
sudo apt upgradeInstall required dependencies for pyenv
sudo apt install make 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-devInstall pyenv
curl -fsSL https://pyenv.run | bashUpdate some environment variables
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrcecho '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrcecho 'eval "$(pyenv init - bash)"' >> ~/.bashrcIf you also have a
.profile file in your home directory, update that file as well. As well as any .bash_profile or .bash_login files.Reset the shell
exec "$SHELL"Or you can logout, and login again.
Install a Python version using pyenv
To see all the commands available in pyenv, type:
pyenv commandsTo get help on a specific command (such as install), type:
pyenv install --helpTo see ALL the versions of Python that pyenv has available for installation, type:
pyenv install --listBut if you're only interested in the CPython versions starting from 3.0, type:
pyenv install --list | grep -E ' 3\.([1-9][0-9]+)'Finally, to install a Python version (e.g., 3.13.7), type:
pyenv install 3.13.7Set the installed Python version as the default
To see all the Python versions that pyenv can manage, type:
pyenv versionsTo see the current Python version that's in use, type:
pyenv versionFinally, the easiest way to set the Python version to use (e.g., 3.13.7), type:
pyenv global 3.13.7pyenv has a couple of other interesting commands for setting the Python version to use depending on whether you just want to use it temporarily in the shell:
pyenv shell 3.13.7or if you want to use a specific version each time you are in a specific folder (e.g., ~/src/proj1):
cd ~/src/proj1pyenv local 3.13.7Now when you switch into that folder, pyenv will make sure to call the correct python version that you specified.
Now all that's left is to...

No comments:
Post a Comment