Created
February 26, 2024 15:27
-
-
Save ulgens/57766250e489da7e8ee9614aa9a2e0f1 to your computer and use it in GitHub Desktop.
Find versions of packages installed in TouchDesigner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pkgutil import iter_modules | |
from pprint import pprint | |
from importlib import import_module | |
from importlib.metadata import version as version_il | |
from importlib.metadata import PackageNotFoundError | |
modules = list(iter_modules()) | |
packages = filter(lambda x: x.ispkg, modules) | |
packages = list(packages) # The filter needs to be iterated more than once | |
packages = sorted(packages, key=lambda p: p.name) # For my personal taste | |
# Problem counters | |
cant_import_count = 0 | |
no_version_count = 0 | |
cant_import = "Can't import" | |
no_version = "No __version__" | |
def find_version(package_name): | |
try: | |
module = import_module(package_name) | |
except ModuleNotFoundError as e: | |
if package_name.startswith("_"): | |
return find_version(package_name.replace("_", "")) | |
else: | |
print(package_name) | |
raise | |
return getattr(module, "__version__") | |
longest_package_name = max(packages, key=lambda x: x.name) | |
# I don't expect anything longer coming from actual versions | |
longest_version = "No __version__" | |
for package in packages: | |
relative_path = package.module_finder.path.split("Contents")[1] | |
try: | |
version = find_version(package.name) | |
except AttributeError: | |
version = no_version | |
no_version_count += 1 | |
except (PackageNotFoundError, ModuleNotFoundError): | |
version = cant_import | |
cant_import_count += 1 | |
package_name = package.name.ljust(20) | |
version = version.ljust(len(longest_version) + 4) | |
print(package_name, version, relative_path) | |
print(f"Can't import: {cant_import_count}") | |
print(f"Can't find version: {no_version_count}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment