Skip to content

An error occurred: module 'importlib.metadata' has no attribute 'packages_distributions' #857

@ReubenFrankel

Description

@ReubenFrankel

This is happening because importlib.metadata.packages_distributions was added in Python 3.10, but the current version check implementation allows this to be called for 3.8 and above:

# TODO(https://github.com/googleapis/python-api-core/issues/835): Remove once we
# no longer support Python 3.7
if sys.version_info < (3, 8):
def _get_pypi_package_name(module_name): # pragma: NO COVER
"""Determine the PyPI package name for a given module name."""
return None
else:
from importlib import metadata
def _get_pypi_package_name(module_name):
"""Determine the PyPI package name for a given module name."""
try:
# Get the mapping of modules to distributions
module_to_distributions = metadata.packages_distributions()
# Check if the module is found in the mapping
if module_name in module_to_distributions: # pragma: NO COVER
# The value is a list of distribution names, take the first one
return module_to_distributions[module_name][0]
else:
return None # Module not found in the mapping
except Exception as e:
print(f"An error occurred: {e}")
return None

Introduced in this commit: d36e896

In our case, this is a problem purely because the error is printed rather than logged, as we are using applications designed around a communication spec that is particularly sensitive to unexpected stdout writes.


Thanks for stopping by to let us know something could be better!

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

Please run down the following list and make sure you've tried the usual "quick fixes":

If you are still having issues, please be sure to include as much information as possible:

Environment details

  • OS type and version: Ubuntu 22.04.5 LTS
  • Python version: Python 3.9.21
  • pip version: 24.3.1
  • google-api-core version: 2.28.1

Steps to reproduce

  1. Import google.api_core in Python REPL
  2. Observed error printed to stdout

Code example

$ .meltano/extractors/tap-bigquery/venv/bin/python
Python 3.9.21 (main, Dec  3 2024, 17:50:13) 
[GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
Ctrl click to launch VS Code Native REPL
>>> import google.api_core
An error occurred: module 'importlib.metadata' has no attribute 'packages_distributions'
/home/reuben/meltano/bigquery-test/.meltano/extractors/tap-bigquery/venv/lib/python3.9/site-packages/google/api_core/_python_version_support.py:252: FutureWarning: You are using a Python version (3.9.21) past its end of life. Google will update google.api_core with critical bug fixes on a best-effort basis, but not with any other fixes or features. Please upgrade to the latest Python version, or at least Python 3.10, and then update google.api_core.
  warnings.warn(message, FutureWarning)
>>>

Stack trace

# example

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions