Python Version Management with Pyenv

Posted in Python

Pyenv

Just as virtual environments are useful to maintain isoated environemnts for project dependencies, Pyenv is a great tool to manage your Python versions in a similar fashion.

Pyenv allows you to jump to different version of Python at the system layer as well as at a project level.

For example, you could have Project A that was built with Python 3.7 and Project B (utilizing data science packages) built with Anaconda.

Real python has a detailed article on Pyenv, but here is how I've used it/plan to use it.

Install Pyenv

My main machine at home is a Mac, so I used Homebrew to install Pyenv with the following...

Home Brew (OS X)

$ brew install pyenv # the actual pyenv application
$ brew install pyenv-virtualenv # plugin for virtual environments

pyenv-installer

I've built a homeserver which has an Ubuntu Server VM on it and I plan to get pyenv up and running in the future. This is how I will install...

$ curl https://pyenv.run | bash

Using Pyenv

Within my ".bash_profile" I added the following code that allows pyenv to operate a bit more seamlessly and handles the activation of virtual environments (more on this later).

.bash_profile

if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv virtualenv-init -)"

Installing a Python Version

After installing Pyenv, the next step is to install Python. To view the list of available pyenv Python versions, you would type...

$ pyenv install --list

There are plenty of Python options to choose from...Python 2/3, anaconda, miniconda, jython, pypy and much more.

Once you settle on a version, you can install using...

$ pyenv install <python-version>

# install python 3.9.0
$ pyenv install 3.9.0

After the Python installation is complete, you can view all the versions on your system using...

$ pyenv versions
* system
  3.9.0

Notice the "*" indicating the Python version currently used. In my case I am using the system version which is Mac OS Python 2.7.16.

If I wanted to change the default Python version at the System level, I would type:

$ pyenv global 3.9.0
$ pyenv versions
  system
* 3.9.0 (set by /Users/matthewjones/.pyenv/version)

After using the versions command again I can see that the global Python is now set to 3.9.0 followed by a trail of who initiated the change.

I've read mix things about changing the global version of Python on a Mac to anything but the system version, so I will change it back.

$ pyenv global system

Also, you can remove the Python version with the following...

$ pyenv uninstall <python_version>

Pyenv Virtualenv

So instead of changing the global version of python, I will adjust the local version. But first I want to create a virtual environment with my newly installed Python version.

$ pyenv virtualenv <python_version> <environment_name>

$ pyenv virtualenv 3.9.0 project
$ pyenv versions
* system (set by /Users/matthewjones/.pyenv/version)
  3.9.0
  3.9.0/envs/project
  project

Once the virtual environment is created with the chosen Python version, you move to your project directory and type:

$ pyenv local project

This will create a ".python-version" file in your current working directory that indicates which version this local project is using.

Since I added the code to my ".bash_profile" file from before, whenever I cd into my project path, the virtual environment will be automatically activated using the Python version used to create that virtual environment.

At this point I can add the needed dependencies for my project using the appropriate package manager (pip, conda, etc.)

...and away we go.