How to switch between Python Versions on MacOS using „pyenv“.

Why do you need to switch between Python Versions?

Well some projects use different versions of python either natively or in their dependencies. Thats where „pyenv“ comes in handy.
With pyenv you can manage python versions global or even locally in your project directory.

How do I install „pynev“?

The easiest way, at least for me, is using Homebrew. Just run the following command.

brew install pyenv

After installing pyenv via Homebrew you can check if the installation was succesfull by opening a new Terminal and run the following.

which pyenv

After that you should get a return like this:

/usr/local/bin/pyenv

If not you have to add some information to yours terminals PATH. Details are descript in the pyenv README.

How do I use „pyenv“ ?

Installing a python version

So how the fun do I use it then? Well first of all you can list the available python version with:
(You just need to run the update to get the latest python versions listed)

brew update && brew upgrade pyenv
pyenv install --list

The return in your terminal will look some what like this.

  ...
  stackless-2.7.14
  stackless-2.7.16
  stackless-3.3.7
  stackless-3.4-dev
  stackless-3.4.2
  stackless-3.4.7
  stackless-3.5.4
  stackless-3.7.5

Pyenv will show you every possible versions of python and the branches.
What you are looking for, at least when you want the „normal“ python versions is this.
You maybe need to scroll up a bit.

  ...
  3.10.8
  3.10.9
  3.11.0
  3.11-dev
  3.11.1
  3.12.0a3
  3.12-dev
  ...

Then you can install the latest stable version of python. In this case 3.11.1.

pyenv install 3.11.1

To check the if the python version was successfully installed, and which versions are also installed run the following.

pyenv versions

The return will look like this.

* system (set by /Users/max/.pyenv/version)
  3.10.4
  3.11.1

Congratulations the python version you wanted was successfully installed.
The star will tell you which version is currently used globally.

Switching python versions

You can select a local python version by running the following.

pyenv local 3.11.1

This will create a „.python-version“ file which tells pyenv what version to use.

To select a global python version with the following command.

pyenv global 3.11.1

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden /  Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden /  Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s