iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
☀

How to switch to any Python version with pyenv

に公開

As the title suggests, this article introduces how to install an arbitrary Python version using pyenv and switch between them depending on the runtime you want to use.
The environment used is Mac.
At the time of writing this article, AWS SAM did not support Python 3.9, so I am putting this together with the intention of installing Python 3.8.

First, install pyenv using Homebrew.

$ brew install pyenv

Once the installation is complete, check the Python versions that can be installed with pyenv.
(This is not necessary if you already know the version you want to specify.)

$ pyenv install -l

In this case, I want to install Python 3.8.8, so I enter the following into the terminal:

$ pyenv install 3.8.8

After installation, switch the default Python version with the following command.
*If it doesn't switch, try restarting your terminal once.

$ pyenv global 3.8.8
$ python --version
  Python 3.8.8

Now you have successfully switched to the installed Python 3.8.8.

As a bonus, let's also install Python 3.7 series.

$ pyenv install 3.7.10

Here, I'll introduce how to switch when multiple versions are installed with pyenv.
You can check the installed versions with the following command.

~ $ pyenv versions
  system
  3.7.10
* 3.8.8 (set by /Users/yourname/.pyenv/version)

In the output above, the version with the asterisk represents the Python version currently in use.
To switch, use the pyenv global command.

$ pyenv global 3.7.10
$ python -V
Python 3.7.10

I was able to confirm that it switched to the specified version.

Discussion