Skip to content

Conversation

@kiruxaspb
Copy link
Member

@kiruxaspb kiruxaspb commented Jan 15, 2024

Changing deprecated pkg_resources favor of importlib.metadata

print("Installed modules:")
print(installed_modules)
return package_name in installed_modules
spec = importlib.util.find_spec(package_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the importlib documentation this function

Find the spec for a module, optionally relative to the specified package name.

So this is not a function that is suitable for use here.
Based on the message from pkg_resources documentation, it is advisable to use the importlib_metadata package here instead of importlib.
A more specific function from this package might be importlib.metadata.metadata.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally the whole function might be:

import importlib.metadata  # importlib_metadata need to be installed


def is_package_installed(package_name):
    try:
        package = importlib.metadata.metadata(package_name)
        name, version = package.json["name"], package.json["version"]
        print(f"Installed {name} {version}")
        return True
    except importlib.metadata.PackageNotFoundError:
        print(f"Package {package_name} is not installed")
        return False

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved in 5db3daf

@kiruxaspb
Copy link
Member Author

@sassless Updated

@kiruxaspb kiruxaspb requested a review from sassless January 26, 2024 11:13
@kiruxaspb kiruxaspb changed the title Change deprecated pkg_resources favor of importlib.util Change deprecated pkg_resources favor of importlib.metadata Jan 26, 2024
@kiruxaspb kiruxaspb merged commit 5ac3547 into singnet:development Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants