Pyenv Setup

Jun 6, 2019 19:05 · 368 words · 2 minute read

How to Setup Pyenv on Mac

pyenv lets you easily switch between multiple versions of Python. It’s simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.

Homebrew - pyenv

Install pyenv:

$ brew install readline xz  # Optional
$ brew install pyenv pyenv-virtualenv

Install different versions of python:

$ pyenv install 3.6.10
$ pyenv install 3.8.1

Option step for python env autoactivation

For fish shell users

$ fisher add fisherman/pyenv  # For fish shell

For zsh shell users

 # Load pyenv automatically by appending
 # the following to ~/.zshrc:
 eval "$(pyenv init -)"

For bash shell users

 # Load pyenv automatically by appending
 # the following to ~/.bash_profile:
 eval "$(pyenv init -)"

To activate Python virtualenv versions manually

 $ source $(pyenv root)/versions/<virtualenv name>/bin/activate

Show which versions of python are installed:

$ pyenv versions

* system (set by /Users/username/Programming/python/.python-version)
  3.6.10
  3.8.1

The asterisk indicates that the system version of python is active, but 3.5.0 and 3.6.0 are also installed.

Pyenv Local

Navigate to demoapp project repo folder, then display the version of python called by bash by entering python -V:

$ python -V
Python 3.8.1

Now enter:

$ pyenv virtualenv 3.6.10 demoapp-py36
$ pyenv local demoapp-py36

This creates a .python-version file which tells pyenv which version of python to run in that directory.

Entering ls -la shows us that file:

$ ls -la
total 8
drwxr-xr-x  3 username  staff   96 Aug  3 11:52 .
drwxr-xr-x  4 username  staff  128 Aug  3 11:56 ..
-rw-r--r--  1 username  staff    6 Aug  3 12:03 .python-version

Now enter pyenv versions:

$ pyenv versions
  system
  3.6.10
* hdp-py36 (set by /Users/username/demoapp/.python-version)
  3.8.1

And running this command shows which version of python is called by pyenv:

$ python -V
Python 3.6.10

To change pyenv to the system version of version 3.6.0 enter:

$ pyenv local system
$ pyenv local 3.6.10

This procedure is fine, you can set a version of python to run in a particular folder. But what if you want to use pyenv to set a global version of python.

Pyenv Global

$ python -V
Python 3.8.1

Now enter:

$ pyenv global 3.6.10

Now enter pyenv versions:

$ pyenv versions
  system
* 3.6.10 (set by /Users/username/.pyenv/.version)
  3.8.1