Note: Be sure your Raspberry Pi is up to date. To do so, run below command in terminal.
sudo apt-get updatesudo apt-get upgrade
Use the pyenv
yet, you can do so using a package manager or by cloning the GitHub repository. Here, I'll provide instructions for using the installation script:
curl https://pyenv.run | bash
echo 'export PATH="$HOME/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
exec "$SHELL"
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
On other systems, the dependencies might be different. Refer to the pyenv wiki for more information.
Use the pyenv install
command to install the Python version you want. For example, to install Python 3.9.12:
pyenv install 3.9.12
Replace 3.9.12
with the version you want to install.
After the installation is complete, you can set the global Python version or set it locally in a specific directory. For example, to set the global version:
pyenv global 3.9.12
To set the version locally in a specific directory, navigate to the directory and run:
pyenv local 3.9.12
Adjust the version number accordingly.
You can verify that the correct Python version is being used by running:
python --version
It should display the version you installed.