This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: AttributeError: 'zipimporter' object has no attribute 'find_spec'
Type: crash Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: desmondcheongzx, mowgli, rudiejd
Priority: normal Keywords:

Created on 2021-01-03 14:09 by mowgli, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg384279 - (view) Author: Klaus Ethgen (mowgli) Date: 2021-01-03 14:09
I encountered the bug when installing a package with `python3 setup.py install --user`. After that, a second installation failed with this stack trace:
```
Traceback (most recent call last):                                                                                                                            
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2191, in _handle_ns                                                                   
    loader = importer.find_spec(packageName).loader                                                                                                           
AttributeError: 'zipimporter' object has no attribute 'find_spec'                                                                                             

During handling of the above exception, another exception occurred:                                                                                           

Traceback (most recent call last):                                                                                                                            
  File "/home/klaus/devel/upstream/pass-git-helper/setup.py", line 1, in <module>                                                                             
    from setuptools import setup                                                                                                                              
  File "/usr/lib/python3/dist-packages/setuptools/__init__.py", line 16, in <module>                                                                          
    import setuptools.version                                                                                                                                 
  File "/usr/lib/python3/dist-packages/setuptools/version.py", line 1, in <module>                                                                            
    import pkg_resources                                                                                                                                      
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3239, in <module>                                                                     
    def _initialize_master_working_set():                                                                                                                     
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3222, in _call_aside                                                                  
    f(*args, **kwargs)                                                                                                                                        
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3264, in _initialize_master_working_set                                               
    tuple(                                                                                                                                              
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3265, in <genexpr>                                                                    
    dist.activate(replace=False)                                                                                                                              
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2780, in activate                                                                     
    declare_namespace(pkg)                                                                                                                                    
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2279, in declare_namespace                                                            
    _handle_ns(packageName, path_item)                                                                                                                        
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2196, in _handle_ns                                                                   
    loader = importer.find_spec(packageName)                                                                                                                  
AttributeError: 'zipimporter' object has no attribute 'find_spec'
```

This error happens now also with different tools.

Solution is to `rm -fr ~/.local/lib`. But the next installation triggers the bug again.

The only solution is to install via `python3 setup.py install --user --old-and-unmanageable`.

When I look into the code of zipimport(er), I do not see any find_spec only the old find_loader.

Despite it is a core library, this must be fixed! And with that the name should match the name of the py file to make it able to find the relevant file.
msg384353 - (view) Author: J.D. Rudie (rudiejd) Date: 2021-01-04 23:05
Is this an issue with zipimporter, or an issue with a version of setuptools? On the latest version of setuptools, find_spec reverts to find_module, which should be implemented in the 3.9 version of zipimporter unless I'm misunderstanding the issue.
msg384431 - (view) Author: Klaus Ethgen (mowgli) Date: 2021-01-05 20:46
I don't know if that is an issue with zipimporter or an issue with a version of setuptools.

My knowledge of python is very limited.

But from what I can see in the backtrace it is missing find_spec. How ever that reverting is or should be implemented, it seams to be obviously that it is not working.
msg384636 - (view) Author: Desmond Cheong (desmondcheongzx) * Date: 2021-01-08 05:10
Could you provide some information about the system you're running this on? I might be wrong, but this does seem like an issue with this copy of setuptools. From the Traceback, it seems that line 2196 of /usr/lib/python3/dist-packages/pkg_resources/__init__.py runs `loader = importer.find_spec(packageName)` when trying to handle the AttributeError exception. However, this line should instead have run `loader = importer.find_module(packageName)` based on the source code https://github.com/pypa/setuptools/blob/95a9c474d30acc729b536f9ad88ead7efab62c5d/pkg_resources/__init__.py#L2190-L2197

Git blame also tells me that this fall-back has been implemented for a few years now.

It also appears that the zipimporter.find_spec method is not implemented in Python3.9 and earlier. However, zipimporter.find_module has been implemented for at least Python3.8 onwards, if not earlier (https://github.com/python/cpython/blob/ca8e96d1edbeb536f58da91e607082463398fce1/Lib/zipimport.py#L139-L150).
msg384640 - (view) Author: Klaus Ethgen (mowgli) Date: 2021-01-08 07:55
You are right.

Generally I have the installation on devuan Linux, the unstable tree.

But... In 2019-03-13 I started to patch this file as many python scripts started to fail due to the fact that they had the find_spec implemented. This was still true in Apr 2020, when I had to renew the patch. And let me say, many failed; not only the egg part.

I don't know where I found the patch at first but it worked now for nearly two years. I did revert it now and for the moment, the scripts that I remember was failing, seems to work now.

Let me say that this find_spec/find_module stuff is (or was) a bit of a mess.
msg384641 - (view) Author: Klaus Ethgen (mowgli) Date: 2021-01-08 07:58
I also tested the original bug and it seems to be gone.
History
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 86984
2021-01-08 07:58:17mowglisetstatus: open -> closed

messages: + msg384641
stage: resolved
2021-01-08 07:55:37mowglisetmessages: + msg384640
2021-01-08 05:10:56desmondcheongzxsetnosy: + desmondcheongzx
messages: + msg384636
2021-01-05 20:46:04mowglisetmessages: + msg384431
2021-01-04 23:05:30rudiejdsetnosy: + rudiejd
messages: + msg384353
2021-01-03 14:09:35mowglicreate