Hi all!
I’m looking for a way to get the Python package name and version, given a path. The path could be the main python packages directory (which contains all the installed Python packages), one of the packages’ directory, some file/directory inside the package directory.
Example to make it more clear: Lets look at /tmp/vendor
. This directory contains all the packages I installed using pip. So for example, if requests
package is installed there, then I have /tmp/vendor/requests/api.py
and other files/directory which are the source code of requests
. Given this file (as example), I want to know that this file is associated with requests
package with 2.26.0
version.
I was trying to use pkg_resources
. That package provides find_distributions
method which can return all the packages inside /tmp/vendor
, by running:
distributions = pkg_resources.find_distributions('/tmp/vendor')
for current_distribution in distributions:
print('Found package', current_distribution .project_name, '==', current_distribution .version)
It didn’t work to run it on /tmp/vendor/requests/api.py` or even
/tmp/vendor/requests`. I’m looking for an algorithm to find a related Python package, given a path (file/directory inside the packages). Is there a way to achieve it?
Please note that this question is the next step of my previous research on the matter.
Thank you in advance.
EDIT: The packages location could be of some Python version. It could be Python3.6, Python2.7 and others. For example, for /tmp/vendor
I used Python3.6 but I want the utility to be generic as possible, given some packages area (that were installed with some specific Python version).