How do I determine the apt package name for a given PyPI package?

Suppose I want to install a Python package system-wide on a Debian/Devuan system. Some PyPI packages have DEB packages available via the repository’s apt sources, with package names prefixed by python3- (or python-, depending on Debian version). But – not all of them do. Now, I can always roll my own DEB package, but – how do I know whether my package of interest is:

  • actually missing;
  • available, but under another name (shorter form, longer-form, traditional name now changed on PyPI etc.); or
  • available, but bundled with other PyPI packages

? Is there a simple mapping rule, or someplace I could query with a PyPI package name and get the apt package name?

Asked By: einpoklum

||

The honest answer is that you will have to make educated guesses or look for the files within. ("Educated guesses" do include doing things like apt search … with the pypi package name)

Checking for the files within: I’d honestly just do the venv installation and then let that show me the contained files. Then, pick a specific file that would seem central and necessarily there, and unique enough, and ask the Debian packaging infrastructure which package contains a file named like that. For example, for the sigmf package:

python3 -m venv temporary_venv
source temporary_venv/bin/activate
pip install sigmf
pip show -f sigmf

From the list of files that the last command gives me, I pick sigmf/sigmffile.py. That sounds like without that file, the package wouldn’t be complete, and also, not a file name I’ll encounter somewhere else.

(you can now just rm -rf temporary_venv and close that terminal. Nothing else was touched.)

So,

# apt-file might need to be installed first, `sudo apt install apt-file`
apt-file update
apt-file find sigmf/sigmffile.py

This yields no results, so there’s no package containing sigmf.

Answered By: Marcus Müller

The most reliable way to find a Debian package matching a given Python package involves knowing how the latter should be imported. For example, pandasql is imported from pandasql; if it was available in a Debian package, it would ship /usr/lib/python3/dist-packages/pandasql.

To determine whether such a package exists, install apt-file if you don’t already have it, and ask that:

sudo apt update
apt-file search /usr/lib/python3/dist-packages/pandasql

If the package exists, apt-file will tell you its name; if it doesn’t find anything, the package doesn’t exist.

Python binary packages should reflect their module name in most cases, but there are caveats:

The binary package for module foo should preferably be named python3-foo, if the module name allows. This is not required if the binary package installs multiple modules, in which case the maintainer shall choose the name of the module which best represents the package.

For the purposes of package naming, the name that is used for a module is the name that can be used with import, which is not necessarily the same as the name used in setuptools PKG-INFO and .egg-info files and directories. For example, the module described in pyxdg-*.egg-info is used via import xdg, so its package name is python3-xdg and not python3-pyxdg.

Answered By: Stephen Kitt