1

How do I canonically determine/find/identify, without having the package installed, which package(s) a given module belongs to (so I can install it)?

I have a legacy code base. It has lots of import statements. If I want to install the referenced modules, say to populate setup.py, I need to know the package names, but there is no necessary relationship between the name of the module and the name of the package. For example, an import cv2 is satisfied by the package opencv-python, not cv2. I'm left with googling, which is less than a satisfying solution.

It seems like this would be a common problem with a well-defined answer, but darned if I can find it. I found an answer that assumes the package is installed, but I want an answer if the package is not installed.

Vercingatorix
  • 1,838
  • 1
  • 13
  • 22
  • You have answered your own question: `there is no necessary relationship between the name of the module and the name of the package`. How could there be a solution to this (frustrating) problem, if there is no defined relationship between import an PyPi module name – FlyingTeller Apr 28 '22 at 07:52
  • @TYZ Yes, I think `pipreqs` is what I'm looking for. If you want to make it an answer, I'll make it accepted. For some reason it comes up with `opencv_python` instead of `opencv-python`, but they appear to be synonymous. – Vercingatorix Apr 28 '22 at 11:42
  • @FlyingTeller By "no defined relationship" I don't mean there isn't a relationship, I mean there is no rule that expresses the relationship such that it is predictable. I.e., while `cv2` maps to `opencv-python` (there is a relationship), nothing in the name `cv2` tells you to look for the package named `opencv-python` (there is no defined relationship). The relationship exists; it's just hidden in the Pypi repository. – Vercingatorix Apr 28 '22 at 11:46

0 Answers0