Raspberry Pi

Switch Multiple Python Versions on Raspberry Pi Using pyenv

As a Raspberry Pi user, you may often put yourself in a situation where a program or package you want to install requires a Python version below 3.9. This is a common problem faced by Raspberry Pi users since the system includes the latest Python version, which not all programs support. Instead of installing the old version from scratch, you can switch to any Python version by installing the “pyenv” utility on your Raspberry Pi system. pyenv makes the Python version-switching process quite straightforward.

Follow this article to install pyenv and easily switch to any python version on the Raspberry Pi system.

Switch Multiple Python Versions on Raspberry Pi Using pyenv

You can install pyenv on Raspberry Pi through the following steps:

Step 1: First, run the pyenv installation script on the Raspberry Pi system through the following command:

$ curl https://pyenv.run | bash

Step 2: Open the environment variable source file using the following command:

$ sudo nano ~/.profile

Then add the following lines in the file.

export PYENV_ROOT="$HOME/.pyenv"

command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"

eval "$(pyenv init -)"

Step 3: Save the file using “CTRL+X”.

Step 4: Apply the following command to reload your changes to the environment variable file.

$ source ~/.profile

Step 5: Now confirms the pyenv installation through the following command:

$ pyenv --version

Step 6: To check for different Python versions, you can run the following pyenv command:

$ pyenv install -l

At the top of the list, you will see different Python versions and you can pick any version you want to install on the Raspberry Pi system.

I am installing Python version 3.8.0 and the command to install this version on the Raspberry Pi system is given below:

$ pyenv install 3.8.0

Note: Ignore the warning or any kind of issue because the above command will successfully install the selected Python version on the Raspberry Pi system.

Step 7: After completing the installation, run the following command to make the selected Python version global for all the users:

$ pyenv global 3.8.0

Step 8: Now, confirm the Python version through the below-given command:

$ python3 --version

The above command ensures that the Python version is successfully switched.

Conclusion

The pyenv is a helpful tool that allows users to switch to any python version within a few seconds. You can install this tool through an installation script. After that, you must add some lines inside the environment variable file and load the changes to install pyenv successfully. Later, you can install any python version using the “pyenv install” command and make the version global to use it on the Raspberry Pi system.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.