PyEnv

Hello Reader,

This post is one of my pdpy-tools series. My intention with it is to give you a brief overview of the pyenv tool for macOS and Linux.

The first thing you should know about pyenv is that it only supports macOS and Linux. The maintainer said that they are not going to spend the time require to support Windows. As an alternative, there is a nacent port of pyenv to windows: pyenv-win. But it specifically states that "Some commands doesn't implemented" and I have not utilized it, so I do not know how well it works.

The second thing you should know about pyenv is that it is hands down the best (and to my mind, only) way to manage multiple versions of Python on macOS and Linux. You can install, uninstall, manually select, and automatically select any supported version of Python.

If you are on macOS, the easiest way to install it is via homebrew:

brew install pyenv

After that you can learn all about what pyenv can do though it's help sub-command:

pyenv help

Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
  commands    List all available pyenv commands
  local       Set or show the local application-specific Python version
  global      Set or show the global Python version
  shell       Set or show the shell-specific Python version
  install     Install a Python version using python-build
  uninstall   Uninstall a specific Python version
  rehash      Rehash pyenv shims (run this after installing executables)
  version     Show the current Python version and its origin
  versions    List all Python versions available to pyenv
  which       Display the full path to an executable
  whence      List all Python versions that contain the given executable

See 'pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme

To install Python 3.7.0 for example:

pyenv install 3.7.0

This will download, compile, and make available Python version 3.7.0 from your home directory. One of the benefits of this is that all installed site-packages will be stored alongside this install of Python as part of it.

The uiltity provided by pyenv does not stop at downloading and compiling multiple versions of Python. It also facilitates usage of those versions in several user directed ways. The first way is though a .python-version file. When you include this file in a directory, it directs pyenv to switch to providing that version when you change into that directory.

$ python --version
Python 2.7.10
$ mkdir demo
$ echo "3.7.2" >> demo/.python-version
$ cd demo
$ python --version
Python 3.7.2

The next way to affect the python version that pyenv provides is through the local, global, and shell. They allow for the Python version provided at those scopes to be set.

Finally, if you run into trouble, your first stop in troubleshooting should be the pyenv wiki. I have yet to encounter a problem that did not have an answer somewhere in that wiki.

Good Luck & Happy Hacking,

=> Charles