classification
Title: ImportError: cannot import name 'MetadataPathFinder' from 'importlib.metadata'
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, Dima.Tisnek, Florimond Manca, Manjusaka, Mariatta, atuining, brett.cannon, iamsav, jaraco, xtreak
Priority: normal Keywords:

Created on 2019-10-01 22:22 by atuining, last changed 2020-02-21 18:05 by Manjusaka. This issue is now closed.

Messages (12)
msg353714 - (view) Author: Anthony Tuininga (atuining) * Date: 2019-10-01 22:22
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)
msg353721 - (view) Author: Александр Семенов (iamsav) Date: 2019-10-02 06:01
on windows
```C:\>py -c "import sys;from importlib.metadata import version, requires, files;print(sys.version);print(version('requests'))"
3.8.0b4 (tags/v3.8.0b4:d93605d, Aug 29 2019, 23:21:28) [MSC v.1916 64 bit (AMD64)]
2.22.0
```
msg353724 - (view) Author: Florimond Manca (Florimond Manca) Date: 2019-10-02 07:51
Can confirm this on 3.8.0rc1+ / macOS Mojave:

```console
$ 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?
msg353725 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-10-02 08:08
The library was synced with https://bugs.python.org/issue38121 which seems to be related to this.
msg353735 - (view) Author: Jason R. Coombs (jaraco) * (Python committer) Date: 2019-10-02 12:35
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.
msg353736 - (view) Author: Jason R. Coombs (jaraco) * (Python committer) Date: 2019-10-02 14:09
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.
msg353739 - (view) Author: Anthony Tuininga (atuining) * Date: 2019-10-02 14:54
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.
msg353740 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2019-10-02 15:02
https://gitlab.com/python-devs/importlib_metadata/issues/92
msg353752 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2019-10-02 16:56
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.
msg360136 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2020-01-16 19:38
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.
msg360150 - (view) Author: Jason R. Coombs (jaraco) * (Python committer) Date: 2020-01-16 22:55
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.
msg362420 - (view) Author: Manjusaka (Manjusaka) * Date: 2020-02-21 18:05
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
History
Date User Action Args
2020-02-21 18:05:42Manjusakasetnosy: + Manjusaka
messages: + msg362420
2020-01-16 22:55:02jaracosetmessages: + msg360150
2020-01-16 19:38:56Mariattasetnosy: + Mariatta
messages: + msg360136
2019-10-03 01:13:22Dima.Tisneksetnosy: + Dima.Tisnek
2019-10-02 16:56:27brett.cannonsetstatus: open -> closed

nosy: + brett.cannon
messages: + msg353752

resolution: not a bug
stage: resolved
2019-10-02 15:02:36BTaskayasetnosy: + BTaskaya
messages: + msg353740
2019-10-02 14:54:08atuiningsetmessages: + msg353739
2019-10-02 14:09:30jaracosetmessages: + msg353736
2019-10-02 12:35:13jaracosetmessages: + msg353735
2019-10-02 08:08:36xtreaksetnosy: + xtreak
messages: + msg353725
2019-10-02 07:51:17Florimond Mancasetnosy: + Florimond Manca
messages: + msg353724
2019-10-02 06:41:31xtreaksetnosy: + jaraco
2019-10-02 06:01:56iamsavsetnosy: + iamsav
messages: + msg353721
2019-10-01 22:22:11atuiningcreate