8

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?

2 Answers 2

15

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.

4
  • that's a fair bit more compact than my answer, so +1, but of course, it's not dealing with diverging package names, versions in the package name etc., well. But you're right, if you know you want to import foobar, then only the package offering dist-packages/foobar/__init__.py will do. Nov 13, 2023 at 13:56
  • I’m not sure I understand your comment — this will find the right package, if it exists, even if the package name “diverges” or includes a version number. Nov 13, 2023 at 13:59
  • yep, but for example, if I try to work on some Python software with a requirements.txt that contains python-imaging, then I need to know that this means the software uses import PIL and then that I need to look for packges containing a dist-packages/PIL/. Sadly, pypi package names don't need to reflect the python module names they contain, necessarily. Nov 13, 2023 at 14:07
  • 1
    Ah, right, thanks. Yes, the lack of nomenclature in PyPI package names does make this more difficult than it could be! Nov 13, 2023 at 14:11
3

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .