Managing multiple Python versions is a pain. This tutorial shows you how to install and use the easiest Python version manager out there: pyenv
June 6th, 2022
raspberry pi
python
If you haven't set up your Raspberry Pi yet, you can follow my tutorial for that here.
Open a terminal to your Pi. You can do this with SSH or directly on your Pi as a desktop. In the terminal, run the command:
curl https://pyenv.run | bash
.bashrc
Next you'll add pyenv to .bashrc
. This lets your terminal know where to look for the pyenv versions of Python. Run the following to open the Nano editor:
sudo nano ~/.bashrc
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
CTRL + S
CTRL + X
exec $SHELL
There's a big list of packages to install. I suggest just copying and pasting this command:
sudo apt-get 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
First, update pyenv with the command
pyenv update
pyenv install --list
pyenv install 3.7.12
You have three options how to set your Python version:
pyenv shell 3.7.12
--unset
flag. This code will reset the Python version for my current terminal directory. pyenv local --unset
rm -fr ~/.pyenv
You also have to remove the lines we added to the .bashrc
file in Step 3.