💾 Archived View for dece.space › dev › faq › using-python-on-debian.gmi captured on 2022-04-29 at 12:33:59. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-11-30)

➡️ Next capture (2024-06-20)

-=-=-=-=-=-=-

Using Python on Debian

Last update: 2021-08-18.

This document is a collection of tips for staying happy while using Python on Debian, as a user or a developer. You should have a bit of experience with working on Debian, or at least Linux concepts and building in general.

Introduction

You'll be happy to know that Debian ships with its own version of Python because several system components depend on Python. This system installation is a critical part of your system so you have to be careful what you do with it!

As of Debian “Bullseye” 11, Python 2 is definitely abandoned and not installed by default anymore, which means the classic command `python` has been removed and only the `python3` command is available.

Installing your own version of Python

If you want to use another version of Python than the one shipped with Debian, you have a few options: using a version manager like pyenv or compiling your own version.

Using a version manager

The most well known Python version manager is pyenv. It lets you install any version of Python you like on your system without issues, and uses a bit of shell magic to let you pick the version you want to use at any moment.

I'll let you refer to the documentation of pyenv if you're interested. I tend to not use version manager (same for NVM in the Node.js world) because I don't like too much magic happening in my shell, it usually comes back to bite you at some point!

pyenv

Compiling Python

It is easy to compile Python on Debian. Download the source on the official site, extract it somewhere, then prepare yourself for the build.

Python sources

First you need to pick the optional dependencies you want to build into your Python installation: ncurses, sqlite, dbm… Python will compile fine without them but some imports won't work. This list install, as far as I know, all the optional libraries needed for a complete installation:

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libgdbm-compat-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev liblzma-dev tcl-dev tk-dev uuid-dev

Then you're ready to build! It is important to note that we use altinstall instead of install to prevent aliases like python3 to be replaced by our new version. For example, if your system ships with Python 3.7, python3 is an alias to python3.7 and we do not want to replace it: altinstall will only create a separate python3.8 or python3.9 executable and not replace any existing binary.

For the make command, the number is the amount of cores to use for the build.

./configure --enable-optimizations
make -j 8
sudo make altinstall

Your new Python will be installed in `/usr/local/{bin,lib}`, whereas the system Python is still in `/usr/{bin,lib}`. It is called python3.x (with x the minor version you installed), along with its own set of versioned tools such as pip3.x, and all distributions and site packages will be kept separately between different minor versions.

Cleaning a Python installation

Python programs use a lot of different libraries and you may wish to remove packages you do not use anymore. You can use pip to list the installed packages along with their location:

pip list -v

On Debian, the locations for Python 3 will usually be one of the following:

To remove packages from A, uninstall them from your package manager, usually something like `sudo apt remove python3-<package>`.

To remove packages from B, uninstall them from pip using the superuser: `sudo pip uninstall <package>`.

To remove packages from C, uninstall them from pip: `pip uninstall <package>`.

Uninstalling your own version of Python

You really don't want to remove the system's installation of Python, but you can uninstall a version you installed yourself.

Using a version manager

Refer to the documentation of your version manager, it should be just a command or two.

Uninstalling a Python installed from source

If you installed Python from source using altinstall, there is no uninstall recipe for make, you have to remove files by hand. Fortunately it's not hard to do. Let's say we want to remove our Python 3.9:

And that should be enough!