How To Manage Multiple Python Installations on Raspberry Pi
2024-09-04 | By Maker.io Staff
For many reasons, you might want to install multiple versions of Python on a Raspberry Pi. For example, some projects require using a library that is only available for an older version of Python. Or you may want to try the newest version without breaking your current installation. Luckily, some tools make managing multiple Python installations a breeze, and this article discusses how to install and configure PyEnv, which is one of them.
Installing PyEnv on a Raspberry Pi
PyEnv is a tool for installing and managing multiple Python versions. It allows downloading and installing specific versions and helps activate or deactivate a particular version on and off. That way, you can decide which installation you want to use globally or within a single session. Installation starts by downloading the installation script and running it in the bash:
curl https://pyenv.run | bash
The installation should not take long, and when done, the installer should display the following message, reminding users to add pyenv to the load path. Open the .bashrc file using a text editor with root privileges:
sudo nano ~/.bashrc
Then, append the following lines to the end of the file:
export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init --path)" eval "$(pyenv virtualenv-init -)"
When using the nano editor, save the file with Ctrl + O and enter. When finished, use Ctrl + X to close the text editor. Next, the bash needs to be restarted before pyenv is operational, which can be done by typing:
exec $SHELL
Updating PyEnv
The PyEnv tool relies on a few external programs that were not automatically installed when downloading the shell script and running it in the console. The following command downloads and installs the required dependencies using the apt package manager:
sudo apt install --yes libssl-dev zlib1g-dev libbz2-dev
libreadline-dev libsqlite3-dev llvm libncurses5-dev
libncursesw5-dev xz-utils tk-dev libgdbm-dev lzma lzma-dev tcl-dev
libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev wget curl make
build-essential openssl
Once done, update PyEnv by running the following command. Doing so ensures that the tool has the most recent list of available installations:
pyenv update
You should then be able to view a list of all available Python versions by typing:
pyenv install --list
Grep can be helpful in limiting the search to a specific version. For example, the following command only displays Python 3 installation candidates:
pyenv install --list | grep '^[[:space:]]*3\.'
pyenv install --list | grep '^[[:space:]]*3\.'
This screenshot shows the result of listing the available Python versions that start with a 3.
A specific version can be installed by using the install command with a version identifier, for example:
pyenv install 3.9.10
Using the versions command with pyenv instructs the program to list all installed Python versions:
This screenshot demonstrates how to list the available Python installations using PyEnv.
While installing more recent versions should go smoothly, older Python releases can cause problems when installed on more modern versions of Raspbian. However, the official PyEnv GitHub repository contains a handy page that lists common errors and how to resolve them.
Managing Python Environments with PyEnv
PyEnv also allows switching between previously installed Python versions. It’s important to note that PyEnv supports three different scopes that define where the selected environment is active.
The local scope uses the selected Python version for the current terminal directory. Every other directory utilizes the default system-wide Python version. Shell activates the defined version for the current terminal session. Once it ends, the system reverts the Python version to the default. Finally, global sets the Python version that should apply globally across all paths and terminal sessions. This setting is retained even after a system reboot, and it’s useful for upgrading the system-wide installation to a newer version that should be used whenever Python runs. For example, the following command sets Python 3.9.10 with the global flag:
pyenv global 3.9.10
Printing the Python version before and after changing environments demonstrates that the switch succeeds:
This screenshot shows how PyEnv changes the active Python version.
Summary
Tools like PyEnv make it easy to install, manage, and switch between multiple Python installations on the same system. By following the steps outlined in this article, you can seamlessly handle various Python versions, install specific versions, and switch between them using different scopes.
The installation process involves running the installation script in the bash, adding PyEnv to the load path by modifying the .bashrc file, installing necessary dependencies using the apt package manager, and updating PyEnv to ensure access to the latest available installations.
Some projects may require the use of a library that is only compatible with an older version of Python, while others may necessitate experimenting with the newest version without affecting the current setup. The functionalities provided by PyEnv, along with virtual environments, let makers tailor their Python environments to specific project needs, making it easier to explore, develop, and deploy Python-based applications and solutions on their Raspberry Pi.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum