Skip to content

ImportError: cannot import name 'MetadataPathFinder' from 'importlib.metadata' #82523

Closed

Description

@atuining
mannequin
BPO 38342
Nosy @brettcannon, @jaraco, @dimaqq, @Mariatta, @Zheaoli, @tirkarthi, @isidentical, @Savier

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2019-10-02.16:56:27.854>
created_at = <Date 2019-10-01.22:22:11.924>
labels = ['3.8', 'type-bug', 'library', 'invalid']
title = "ImportError: cannot import name 'MetadataPathFinder' from 'importlib.metadata'"
updated_at = <Date 2020-02-21.18:05:42.813>
user = 'https://bugs.python.org/atuining'

bugs.python.org fields:

activity = <Date 2020-02-21.18:05:42.813>
actor = 'Manjusaka'
assignee = 'none'
closed = True
closed_date = <Date 2019-10-02.16:56:27.854>
closer = 'brett.cannon'
components = ['Library (Lib)']
creation = <Date 2019-10-01.22:22:11.924>
creator = 'atuining'
dependencies = []
files = []
hgrepos = []
issue_num = 38342
keywords = []
message_count = 12.0
messages = ['353714', '353721', '353724', '353725', '353735', '353736', '353739', '353740', '353752', '360136', '360150', '362420']
nosy_count = 10.0
nosy_names = ['brett.cannon', 'jaraco', 'atuining', 'Dima.Tisnek', 'Mariatta', 'Manjusaka', 'xtreak', 'BTaskaya', 'iamsav', 'Florimond Manca']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue38342'
versions = ['Python 3.8']

Activity

  1. atuining commented on Oct 1, 2019

    atuiningmannequin
    MannequinAuthor

    Running the suggested code found at https://docs.python.org/3.8/whatsnew/3.8.html regarding the new importlib.metadata module

    from importlib.metadata import version, requires, files
    version('requests')

    yields the error

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/lib/python3.8/importlib/metadata/__init__.py", line 365, in version
        return distribution(package).version
      File "/usr/local/lib/python3.8/importlib/metadata/__init__.py", line 338, in distribution
        return Distribution.from_name(package)
      File "/usr/local/lib/python3.8/importlib/metadata/__init__.py", line 159, in from_name
        dists = resolver(name)
      File "<frozen importlib._bootstrap_external>", line 1381, in find_distributions
    ImportError: cannot import name 'MetadataPathFinder' from 'importlib.metadata' (/usr/local/lib/python3.8/importlib/metadata/__init__.py)
  2. added
    3.8 (EOL)
    stdlib
    type-bug
    on Oct 1, 2019
  3. Savier commented on Oct 2, 2019

    Saviermannequin
    Mannequin

    on windows

    3.8.0b4 (tags/v3.8.0b4:d93605d, Aug 29 2019, 23:21:28) [MSC v.1916 64 bit (AMD64)]
    2.22.0
    
  4. FlorimondManca commented on Oct 2, 2019

    FlorimondMancamannequin
    Mannequin

    Can confirm this on 3.8.0rc1+ / macOS Mojave:

    $ python -c "import importlib.metadata; print(list(importlib.metadata.distributions()))"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/Users/florimond/.pyenv/versions/3.8-dev/lib/python3.8/importlib/metadata/__init__.py", line 175, in <genexpr>
        resolver()
      File "<frozen importlib._bootstrap_external>", line 1381, in find_distributions
    ImportError: cannot import name 'MetadataPathFinder' from 'importlib.metadata' (/Users/florimond/.pyenv/versions/3.8-dev/lib/python3.8/importlib/metadata/__init__.py)

    I discovered this pytest started failing on 3.8-dev because it uses importlib.metadata.distributions() internally.

    3.8.0b4+ used to be fine I believe. I'd assume this is a rather critical bug?

  5. tirkarthi commented on Oct 2, 2019

    @tirkarthi
    Member
  6. jaraco commented on Oct 2, 2019

    @jaraco
    Member

    I believe importlib/metadata/init was moved to importlib.metadata. I’ll inspect the repo for correctness, but there may be another issue with the old files lingering.

  7. jaraco commented on Oct 2, 2019

    @jaraco
    Member

    I've confirmed that the metadata directory doesn't exist in the repo. It also doesn't appear on my Python 3.8.0b4 installation on macOS:

    cpython master $ ls /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/importlib/                                               
    __init__.py            _bootstrap.py          abc.py                 metadata.py            util.py
    __pycache__            _bootstrap_external.py machinery.py           resources.py
    cpython master $ python3.8 -V                                                                                                                
    Python 3.8.0b4
    

    I installed b4 clean. I also installed 3.8.0rc1 over 3.8.0b4 and everything is fine.

    It's possible that installing 3.8.0b4 over 3.8.0b3 would leave the old package lingering. Can you confirm that's how 3.8.0rc1 was installed?

    I've crafted this command which should correct the situation in such an environment::

    python -c "import importlib.metadata, shutil, pathlib; file = pathlib.Path(importlib.metadata.__file__); str(file).endswith('__init__.py') and shutil.rmtree(file.parent) and print('removed', file.parent)"

    Please give that a try and see if it corrects the condition.

  8. atuining commented on Oct 2, 2019

    atuiningmannequin
    MannequinAuthor

    Yes. I had tried b3 earlier, installed b4 over b3 and then rc1 over b4. Removing the cruft using the command you specified caused the problem to go away.

  9. brettcannon commented on Oct 2, 2019

    @brettcannon
    Member

    Closing as not a bug as this seems to be an issue from installing over a b3 or earlier build where the importlib/metadata/ directory gets left behind and thus take priority in import over importlib/metadata.py.

  10. Mariatta commented on Jan 16, 2020

    @Mariatta
    Member

    Just wanted to add that I got the same error in Python 3.8.1.

    I ran the script that @jaraco wrote above:

    python -c "import importlib.metadata, shutil, pathlib; file = pathlib.Path(importlib.metadata.__file__); str(file).endswith('__init__.py') and shutil.rmtree(file.parent) and print('removed', file.parent)"

    I wonder if the fix (e.g. running the script above) should be more visible, perhaps in some documentation somewhere, instead of having to dig this up from the bugtracker.

    Is it possible for the Python installer itself to execute that script?

    Thanks.

  11. jaraco commented on Jan 16, 2020

    @jaraco
    Member

    Given that this issue only affects those who upgraded from beta versions, I'm inclined to say it shouldn't be part of the installer, and that the long tail of users affected probably can track it down here.

    I don't feel strongly about it though.

  12. Zheaoli commented on Feb 21, 2020

    Zheaolimannequin
    Mannequin

    Hello Mariatta

    I have tested the code below on 3.8.1 what's installed by pyenv on my Mac

    from importlib.metadata import version, requires, files
    version('requests')

    it works correctly.

    I think it depends on the tool what's used to install python

  13. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.8 (EOL)stdlibtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions