Found it out myself, turns out that pkg_resources.working_set
contains all installed projects currently available. From there on out, I was able to kind of figure out which imports a project provides, and worked backwards through the list.
My current code looks like this:
def to_package_list(imports):
pkg_map = {}
all_packages: pkg_resources.WorkingSet = pkg_resources.working_set
for package in all_packages:
p: pkg_resources.Distribution = package
f = os.path.join(
p.location,
f"{p.project_name.replace('-','_')}-{p.version}.dist-info",
"RECORD",
)
if os.path.exists(f) and os.path.isfile(f):
with open(f, "r") as file:
lines = file.read().splitlines()
imported_names = [x.strip().split("/")[0] for x in lines]
filtered_imported_names = []
for n in imported_names:
if "." not in n and n not in filtered_imported_names:
filtered_imported_names.append(n)
for n in filtered_imported_names:
pkg_map[n] = p.project_name
return [{"proj": pkg_map[x] if x in pkg_map else x, "imp": x} for x in imports]
This seems to work relatively well. If anyone else has better solutions than this, please let me know.