Setting Up a Modern Python Development Environment on Ubuntu 20.04

Table of Contents

Introduction

Setting up a Python development environment can be daunting. For some, this setup will be overkill, but for a professional project, I’ve found these tools work well. The only prerequisite is an installation of Ubuntu 20.04. These instructions could be used on a different operating system or version of Ubuntu, but the specifics might change.

Tools

We’ll install the following tools.

  • Pyenv helps us manage the version of our Python interpreters and allows us to change our default Python interpreter to one that is not the system interpreter.
  • Pipx allows us to install CLI tools in isolated environments. Instead of installing the tools to the system interpreter or having to switch between various virtual environments, pipx installs the CLI tools in an isolated environment and exposes them for usage.
  • Poetry manages our Python dependencies. Poetry ensures that the same versions of our Python packages are installed across environments such as develop, staging, and production. Poetry can also be used to publish Python packages.

Tool Explanation

Let’s start with an example scenario to demonstrate the use for the tools. Suppose that I’m working on a Django web application. The Django project uses Python 3.7 and Django 2.2, however, our project lead wants to upgrade to Python 3.8 and Django 3.1. Additionally, AWS hosts our Django project.

Pyenv

Pyenv helps us by installing Python 3.7 on our Ubuntu 20.04 system. The default system version is 3.8.2 which doesn’t work for our current project. Pyenv allows us to install the latest 3.7 for our Django project’s development environment. Also, once our project upgrades to Django 3.1 and Python 3.8, we use pyenv to install Python 3.8.5 so that we don’t use the system interpreter.

Pipx

As I mentioned, AWS hosts our Django project. Occasionally, we’ll need to interact with AWS via CLI. To do this, we’ll use the awscli package from AWS. The awscli program is a Python CLI utility, and if, after setting up pyenv we pip install awscli, awscli installs into our global pyenv interpreter. This situation is better than installing the awscli to the system interpreter but not ideal. Ideally, we isolate awscli into its own virtual environment. However, we would have to activate that virtual environment every time we want to run the CLI. Instead of doing this, we install pipx and use pipx to install the awscli. This allows us to run the aws command from anywhere and isolates the awscli utility into its own environment.

Poetry

Generally, poetry replaces pip. Instead of pip install <package>, you would poetry add <package>. Poetry gives us a few extra features that pip doesn’t, namely, a record of top level project dependencies, a separate record of top level development dependencies, a lock file which locks all dependency versions, and the ability to publish Python packages. In this case, these features make managing our Django project’s dependencies easier. Poetry automatically pegs our dependencies to the latest major version so that upgrading our dependencies is safer.

Walk Through

Installing Pyenv

  1. Install the prerequisite packages for Ubuntu here.

  2. Install pyenv using the installer here.

  3. Follow the instructions here to update your .bashrc with the pyenv initialization step and environment variable.

  4. Restart your shell: exec $SHELL.

  5. Validate the installation: pyenv --version.

    pyenv version

  6. Install your preferred version of Python: pyenv install 3.8.5

    • Note: this could take some time.

    pyenv python install

  7. Set the newly installed python as your global interpreter: pyenv global 3.8.5

  8. Validate your global interpreter is the newly installed Python: which python

    pyenv install validate

Installing Pipx

  1. Install pipx via pip here.

  2. Once you’ve installed pipx, restart your shell: exec $SHELL.

  3. Validate the pipx installation: pipx --version.

    pipx install validate

  4. Let’s install the awscli as a test: pipx install awscli.

    pipx awscli install

  5. Validate the awscli install: aws --version.

    pipx awscli install validate

Installing Poetry

Important note: I recommend not installing poetry with pipx. For in depth reasons, see here.

  1. Install poetry using the recommended installer here.

    poetry install

  2. Update your shell: source $HOME/.poetry/env.

  3. Validate the installation: poetry --version.

    poetry install validate

Final Thoughts

Once again, this tutorial uses Ubuntu 20.04 as the base, but it should be usable for any other operating system or version of Ubuntu as well. The specific commands might change, but the ideas are the same. Some of these tools are newer to the Python world, but they’re gaining adoption. I found that once I understood the purpose of these tools they helped simplify my Python development process.

Steven Pate
Steven Pate
Founder

Senior Software Engineer who likes Python and has some social skills