Maker.io main logo

How To Manage Multiple Python Installations on Raspberry Pi

2024-09-04 | By Maker.io Staff

Raspberry Pi

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:

Copy Code
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:

Copy Code
sudo nano ~/.bashrc

Then, append the following lines to the end of the file:

Copy Code
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:

Copy Code
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:

Copy Code
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:

Copy Code
pyenv update

You should then be able to view a list of all available Python versions by typing:

Copy Code
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:

Copy Code
pyenv install --list | grep '^[[:space:]]*3\.'
pyenv install --list | grep '^[[:space:]]*3\.'

How To Manage Multiple Python Installations on Raspberry Pi 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:

Copy Code
pyenv install 3.9.10

Using the versions command with pyenv instructs the program to list all installed Python versions:

How To Manage Multiple Python Installations on Raspberry Pi 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:

Copy Code
pyenv global 3.9.10

Printing the Python version before and after changing environments demonstrates that the switch succeeds:

How To Manage Multiple Python Installations on Raspberry Pi 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.

制造商零件编号 SC0194(9)
RASPBERRY PI 4 B 4GB
Raspberry Pi
¥447.70
Details
制造商零件编号 SC1111
SBC 2.4GHZ 4 CORE 4GB RAM
Raspberry Pi
¥488.40
Details
制造商零件编号 SC0195(9)
RASPBERRY PI 4 B 8GB
Raspberry Pi
¥610.51
Details
制造商零件编号 SC1112
SBC 2.4GHZ 4 CORE 8GB RAM
Raspberry Pi
¥651.21
Details
制造商零件编号 SC0373
SBC 1.8GHZ 4 CORE 4GB RAM US
Raspberry Pi
¥569.81
Details
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum