Skip to content

Instantly share code, notes, and snippets.

@plembo
Last active February 22, 2024 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plembo/52801908305c02d3680b55608fbdb19c to your computer and use it in GitHub Desktop.
Save plembo/52801908305c02d3680b55608fbdb19c to your computer and use it in GitHub Desktop.
Managing python

Managing python (for most of us)

I was recently inspired by an article entitled "Relieving your Python packaging pain" to rethink how I manage python on my personal machines. Keep in mind that I'm a systems architect by trade, not a software developer, and so your mileage may vary.

Rules of the game

In the above mentioned article, six basic rules were set out that I can wholeheartedly agree with:

  1. Don't install the latest major version of Python
  2. Use only the python.org installer on Windows and Mac, or official repositories on Linux
  3. Never install or run anything outside of a virtual environment
  4. Limit yourself to the basics: "pip" and "venv"
  5. If you run a command, use "-m"
  6. When creating a virtual environment, be explicit about the python version (e.g. "python3.11" or "python3.10) you use

Nobody has time for Python, 27 March 2023, https://bitecode.substack.com/p/relieving-your-python-packaging-pain.

Read the article for more detail. A follow on piece, "Why not tell people to 'simply' use pyenv, poetry or anaconda", goes into depth on why to avoid those listed tools, and more. Nobody has time for Python, 30 March 2023, https://bitecode.substack.com/p/why-not-tell-people-to-simply-use.

My setup

On Linux

Although my home servers run Debian 11 Stable (Bookworm), which ships with python 3.11, my desktop and laptop are Ubuntu 22.04 LTS (NVIDIA cards onboard), which have 3.10.12. Not the latest, but still adequate. What follows has been tested on both Debian and Ubuntu.

In the past I used pyenv to manage multiple versions, but my current work doesn't require that. I still think that pyenv represents the best solution for those who do have to manage multiple versions of python, or are in need of the latest releases.

These are the packages I install for a minimal install:nae

sudo apt install -y \
python3-venv \
python3-distutils \
python3-lib2to3 \
python3-setuptools \
python3-tk \
build-essential \
libssl-dev \
libffi-dev \
python3-dev \
python3-pip

On Windows

There was a time when I would only use python in a Linux virtual machine on Windows (e.g., a WSL console or VMware Player). But that isn't practical or desirable for many people. To install directly on Windows I use the next to latest major version Windows Installer (64-bin) from https://python.org/downloads. By default this puts python to the installing user's home directory, avoiding the need for administrator privileges.

Using virtual environments

For a number of years, I've disciplined myself to use virtual environments in every python project. I have a top level folder for projects in my home directory, under which all projects live. Each python project has its own .venv folder set up with python -m venv .venv. To create a python 3.11 virtual environment I use python3.11 -m venv .venv. I now keep a 3.11 virtual environment in the root of my home directory for use with non-projects.

While I use virtual environments wherever practical, additional python libraries do occasionally get installed as dependencies for other software. I never use pip (or python -m pip) outside a virtual environment.

The one concession to customization was to install the python-is-python3 package several months ago, because I am well and truly done with python 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment