简体   繁体   中英

What's the equivalent for "rpm -qf <FILE>" with the Python rpm library?

# rpm -qf /usr/lib64/python3.7/site-packages/six.py
python3-six-1.12.0-r0

How do I use Python to query which package the file belongs to?

I was going to say Google it, but it actually doesn't return results. I guess it's a very unorthodox way of approaching the problem/asking the question.

Python default uses pip to install packages. Find the documentation here

Another option is easy_install , and many recommend doing it through conda which requires Anaconda and requires more set-up but manages a nice controlled Python environment to manage versions within.

Google should show you those.

I've not found a direct call to the rpm database that gives in return the package providing a given file, so the better solution I've right now is the following:

#!/usr/bin/env python3

import rpm

CHECK_FILE = "/usr/lib/python3.9/site-packages/six.py"

ts = rpm.TransactionSet()
mi = ts.dbMatch()
for h in mi:
    files = h['FILENAMES']
    if CHECK_FILE in files:
        print("{}-{}-{}".format(h['name'], h['version'], h['release']))
        break

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM