Search code examples
pythonpip

How to find the package name for a specific module?


In cases where we have python code, but no instructions about which packages to install via pip before running the code, and we run it:

import inception5h

ERROR: Could not find a version that satisfies the requirement inception5h (from versions: none)
ERROR: No matching distribution found for inception5h

Is it safe to guess - i.e. try something like pip install inception5h or pip install inception?

I assume that in absence of specific instructions about which package to install, we are simply guessing, since (I think), any package could theoretically have modules of any name (that is, there could be numerous packages with a module named inception5h).

Is this understanding correct? And are there any clever ways of figuring out which package needs to be installed, preferably without guessing?


Solution

  • Update May 2021

    This should nowadays be relatively straightforward with importlib_metadata.packages_distributions().

    See the documentation here: stdlib or external.


    Original answer

    That I know of, there is no straightforward (non-guess) way to deduce the name of a (PyPI) distribution package name when all you have is the name of an import package (or module). As explained in the this answer for example (linked by @hoefling in the comments to your question).

    There is obviously the convention of having in each distribution package 1 top-level importable package (or module) with the same name.

    So, for example pip install requests allows you to write the Python code import requests.

    But it is just a convention and obviously there are outliers:

    • Some have more than 1 top-level importable package, for example setuptools:
      • pip import setuptools
      • import setuptools and import pkg_resources
    • Some have different names entirely, for example the project PyYAML:
      • pip install pyyaml
      • import yaml

    In such a case, I think I would probably try to look up the name of the import package in a web search engine and hope to find some results leading to the name of the distribution package.