Reputation: 14978
I know there is a command pip show
for the purpose but I would like to know whether it is possible I can fetch details by doing import pip
? When you run pip show
it gives info like:
pip show requests
Name: requests
Version: 2.26.0
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: [email protected]
License: Apache 2.0
Location: /anaconda3/lib/python3.7/site-packages
Requires: urllib3, certifi, charset-normalizer, idna
Required-by: yarg, wordpress-api, WooCommerce, web3, vvm, tweepy, tika, stellar-sdk, stellar-base-sseclient, Sphinx, spacy, smart-open, requests-toolbelt, requests-oauthlib, requests-html, python3-nmap, python-binance, pyArango, poetry, pigar, pandas-datareader, MechanicalSoup, mara-pipelines, kubernetes, ipfshttpclient, hdfs, google-cloud-storage, google-api-core, discum, conda, conda-build, ccxt, CacheControl, browsermob-proxy, bravado, apache-beam, anaconda-project, anaconda-client, alpha-vantage, facebook-sdk
I need data in Required-by
field.
Upvotes: 4
Views: 1082
Reputation: 914
import pip
def show(package):
if hasattr(pip, 'main'):
pip.main(['show', package])
else:
pip._internal.main(['show', package])
show('requests')
Or better to use current runtime:
import subprocess
import sys
def show(package):
subprocess.check_call([sys.executable, "-m", "pip", "show", package])
show('requests')
Answer is inspired (overworked) by this answer
Upvotes: 0
Reputation: 3536
Playing with pip source code, I found the following solution which works for Python 3.8.1 and pip 21.0.1 .
from pip._internal.commands.show import search_packages_info as search_packages_info
package_name='requests'
# here I use next, because search_packages_info returns a generator
package_info=next(search_packages_info([package_name]))
required_by=package_info['required_by']
Example output (it is dependent on the used python environment)
['requests-unixsocket', 'pysolr', 'jupyterlab-server']
Upvotes: 5