The Noble Python Eightfold Path

The way to Python Nirvana is through proper isolation.

Subtext: do not mind the pseudo-spiritual undertones. Take it with a grain of salt - I’m doing a bit.

The Four Noble Truths

  1. Python package and environment management has inevitable suffering

  2. There is a cause to our suffering - entangement of environments

  3. There is an end to our suffering - isolate python environments

  4. The Python Eightfold Path is the way to end suffering and achieve Python Nirvana

The Eightfold Path

  1. Right Version - Use the python version that works for you. For some, this may be the bleeding edge, others may want to hold back for stability and library availability. Avoid python2, for it is the past. Python3 is the present and the future.

  2. Right Installation - Use pyenv to manage your python installations. Avoid using system or package-manager-installed python for development. Use pipx to install stand-alone tools and utilities such as poetry, black, mypy, virtualenvwrapper, etc.

  3. Right Environment - Always use an isolated environment, be it virtualenv, conda, poetry env, PEP-582, or containers†. Avoid using pip install --user (unless to install pipx). Never sudo pip install. Ever. sudo pip is the way to eternal suffering.

  4. Right Isolation - Keep your environments isolated. LD_LIBRARY_PATH is a necessary evil at times (typically some GPU libraries), but beyond that, keep your environment self-contained. Editable installs are a double-edged sword, they are incredibly useful, and you should use them, but be aware that they may complicate your environment. Learn to use sys.path to track down sources.

  5. Right Structure - Use installable packages, rather than stand alone .py scripts. Use modules, with a folder structure that matches the import namespace. Leverage python -m modulename, entry points, and __main__.pyfiles. PYTHONPATH is often a code smell.

  6. Right Tooling - Python has a rich ecosystem of tools for packaging, building, publishing, formatting, linting, style enforcement, and type checking. Discover them and take advantage of the ones that work with your philosophy.

  7. Right Dependencies - Python is well-known for its batteries-included standard library, and sprawling expanse of open-source libraries. However, every dependency is a liability, and managing dependency versions is an art. Poetry and pipenv are helpful for keeping the dependency graph of applications consistent. Use lockfiles for applications, but not libraries.

  8. Right Testing - Exercising your code in a variety of environments is the key to robustness. Your code should not care what current working directory it is run from, if it does, revisit structure. If you are writing a library, test with a matrix of python versions, operating systems, and architectures.

† Which isolation method should I use?

This is a matter of personal preference. Some argue that you don’t really need a virtualenv, however they may reconsider when each repository wants its own multi-gigabyte machine learning stack. PDM purports to avoid “nested venvs”, however pipx does an even better job at this, by allowing python utilities to be installed user-wide or system-wide.
Personally, I use pipx to manage all my CLI tools (poetry, black, virtualenvwrapper), poetry for dependency management, and put everything else in virtualenvs based on a pyenv-installed python. Sometimes I use brew, but it tends to automatically change python versions on me unexpectedly. Some folks swear by anaconda, I swear at it. I use docker heavily for application deployment, but I prefer developing locally in a virtualenv, it’s just easier and less messy to work with Pycharm and Jupyter. But some folks only like to develop in containers. The point is, I can’t tell you the best isolation technique, you must discover that for yourself.

Written on October 8, 2021