0

I'm getting a very strange error message when I run a python module. The interpreter will not even run the first lines of my code but will immediately do some sort of python test which fails. Here is the full trackback

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/kylefoley/Documents/codes/venv3/lib/python3.8/site-packages/pytest/__init__.py", line 5, in <module>
    from _pytest.assertion import register_assert_rewrite
  File "/Users/kylefoley/Documents/codes/venv3/lib/python3.8/site-packages/_pytest/assertion/__init__.py", line 9, in <module>
    from _pytest.assertion import rewrite
  File "/Users/kylefoley/Documents/codes/venv3/lib/python3.8/site-packages/_pytest/assertion/rewrite.py", line 34, in <module>
    from _pytest.assertion import util
  File "/Users/kylefoley/Documents/codes/venv3/lib/python3.8/site-packages/_pytest/assertion/util.py", line 13, in <module>
    import _pytest._code
  File "/Users/kylefoley/Documents/codes/venv3/lib/python3.8/site-packages/_pytest/_code/__init__.py", line 2, in <module>
    from .code import Code
  File "/Users/kylefoley/Documents/codes/venv3/lib/python3.8/site-packages/_pytest/_code/code.py", line 33, in <module>
    import pluggy
  File "/Users/kylefoley/Documents/codes/venv3/lib/python3.8/site-packages/pluggy/__init__.py", line 16, in <module>
    from .manager import PluginManager, PluginValidationError
  File "/Users/kylefoley/Documents/codes/venv3/lib/python3.8/site-packages/pluggy/manager.py", line 6, in <module>
    import importlib_metadata
  File "/Users/kylefoley/Documents/codes/venv3/lib/python3.8/site-packages/importlib_metadata/__init__.py", line 466, in <module>
    __version__ = version(__name__)
  File "/Users/kylefoley/Documents/codes/venv3/lib/python3.8/site-packages/importlib_metadata/__init__.py", line 433, in version
    return distribution(package).version
  File "/Users/kylefoley/Documents/codes/venv3/lib/python3.8/site-packages/importlib_metadata/__init__.py", line 406, in distribution
    return Distribution.from_name(package)
  File "/Users/kylefoley/Documents/codes/venv3/lib/python3.8/site-packages/importlib_metadata/__init__.py", line 175, in from_name
    dists = resolver(name)
  File "<frozen importlib._bootstrap_external>", line 1382, in find_distributions
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/metadata.py", line 379, in find_distributions
    found = cls._search_paths(context.pattern, context.path)
AttributeError: 'str' object has no attribute 'pattern'

Here is the actual method that is failing:

class MetadataPathFinder(DistributionFinder):
    @classmethod
    def find_distributions(cls, context=DistributionFinder.Context()):
        """
        Find distributions.

        Return an iterable of all Distribution instances capable of
        loading the metadata for packages matching ``context.name``
        (or all names if ``None`` indicated) along the paths in the list
        of directories ``context.path``.
        """
        found = cls._search_paths(context.pattern, context.path)
        return map(PathDistribution, found)

I have two modules, both begin with the exact same two lines of code which are

import copy
import sys

One of the module works the other does not. The strange part is that on the failing module the first two lines of code do not every run. Further, I'm using Pycharm and there are no flags on the module that is not working.

1
  • You're trying to pass a string into the context parameter. Can you send the code you wrote? Where have you used that method? Commented Apr 11, 2022 at 22:57

1 Answer 1

1

Checked at python 3.10

from importlib.metadata import DistributionFinder as D

instance = D.Context()

dir(instance)
['__class__', ..., '__weakref__', 'name', 'path']

DistributionFinder.Context() instance doesn't have pattern attribute now.

Newer importlib code is:

class MetadataPathFinder(DistributionFinder):
    @classmethod
    def find_distributions(cls, context=DistributionFinder.Context()):
        """
        Find distributions.

        Return an iterable of all Distribution instances capable of
        loading the metadata for packages matching ``context.name``
        (or all names if ``None`` indicated) along the paths in the list
        of directories ``context.path``.
        """
        found = cls._search_paths(context.name, context.path)
        return map(PathDistribution, found)

Your importlib is outdated. Moreover importlib library is getting from default system version path (Library/Frameworks/Python.framework/Versions/3.8/) but python interpreter is getting from venv path (/Users/kylefoley/Documents/codes/venv3)

You need to downgrade python interpreter version or upgrade importlib library version. Related issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.