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.
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']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With