2

If I install some python pacakges, like opencv-python, it will install the cv2 pacakge.

But before I look at opencv's document, how can I inspector the opencv-python pacakge, and find out what it installed.

Like pip info opencv-python, but it will not print the installed packages.

Update:

I find the pip installed place /usr/local/lib/python3.11/site-packages/opencv_python-4.7.0.72.dist-info/top_level.txt contains the top_level package(like installed packages). So I can write some script to parse this file, or python has builltin util to print this info?

user3875388
  • 541
  • 1
  • 6
  • 19

3 Answers3

2

The answers demonstrating pip show usage are fine if you just wanted to discover this information from the command-line interactively. If you want access to the top-level package names programmatically, however, pip is not much help because it has no API.

Since Python 3.10 the information is easily available using stdlib importlib.metadata:

>>> from importlib.metadata import packages_distributions
>>> dists = packages_distributions()
>>> [name for name, pkgs in dists.items() if "opencv-python" in pkgs]
['cv2']

If you want to know all the files owned by an installed package, not just the top-level import names, you can get that too like this:

from importlib.metadata import distribution
dist = distribution("opencv-python")
for file in dist.files:
    print(file)
    ...
wim
  • 338,267
  • 99
  • 616
  • 750
1

pip show <packagename> will tell you where it's installed in the Location field (docs)

You could just list out the content, if that's all you're after, for example

find "$(pip show requests | grep Location | cut -d' ' -f2)/requests"

You can also use pip freeze to see what packages are installed


It's not possible to know in advance without looking at the code to see what a Python package will install because it needs to parse (normally run) setup.py
However, see Is there a way to list pip dependencies/requirements? which suggests johnnydep by wim which seems up-to-date

ti7
  • 16,375
  • 6
  • 40
  • 68
  • Note: this only works because `requests` names the top-level package also `requests`. But `opencv-python` from the question doesn't do that. – wim Jul 19 '23 at 04:14
1

If you run pip show -f <PACKAGE>—that is, pip show with the -f (or --files) flag, it will list the contents of the package. For example,

pip show -f opencv-python

or

pip show --files opencv-python

will list all the files installed by the package.

mipadi
  • 398,885
  • 90
  • 523
  • 479