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: ImportError: bad magic number in ... does not indicate where is that file located
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, hroncok, petr.viktorin
Priority: normal Keywords: patch

Created on 2022-02-23 12:43 by hroncok, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 31534 open hroncok, 2022-02-23 17:48
Messages (3)
msg413788 - (view) Author: Miro Hrončok (hroncok) * Date: 2022-02-23 12:43
Recently I've been debugging a very nasty bug report that looked like this:

    Traceback (most recent call last):
      File "/usr/bin/jupyter-notebook", line 5, in <module>
        from notebook.notebookapp import main
      File "/usr/lib/python3.10/site-packages/notebook/notebookapp.py", line 78, in <module>
        from .services.kernels.kernelmanager import MappingKernelManager, AsyncMappingKernelManager
      File "/usr/lib/python3.10/site-packages/notebook/services/kernels/kernelmanager.py", line 18, in <module>
        from jupyter_client.session import Session
      File "/usr/lib/python3.10/site-packages/jupyter_client/session.py", line 41, in <module>
        from jupyter_client.jsonutil import extract_dates, squash_dates, date_default
      File "/usr/lib/python3.10/site-packages/jupyter_client/jsonutil.py", line 10, in <module>
        from dateutil.parser import parse as _dateutil_parse
      File "/usr/lib/python3.10/site-packages/dateutil/parser/__init__.py", line 2, in <module>
        from ._parser import parse, parser, parserinfo, ParserError
      File "/usr/lib/python3.10/site-packages/dateutil/parser/_parser.py", line 42, in <module>
        import six
    ImportError: bad magic number in 'six': b'\x03\xf3\r\n'

For details, see https://bugzilla.redhat.com/2057340 and https://github.com/benjaminp/six/issues/359


What would really make things much easier to understand would be if the exception mentioned what is the path of 'six'.


Consider this example:

A rogue .py file in /usr/bin:

    $ sudo touch /usr/bin/copy.py

Programs fail with:

    Traceback (most recent call last):
      File "/usr/bin/...", line ..., in <module>
        ...
    ImportError: cannot import name 'deepcopy' from 'copy' (/usr/bin/copy.py)

Immediately I can see there is /usr/bin/copy.py which is probably not supposed to be there.

However, when it is a pyc instead:

    $ sudo touch /usr/bin/copy.pyc

Programs fail with:

    Traceback (most recent call last):
      File "/usr/bin/...", line ..., in <module>
        ...
    ImportError: bad magic number in 'copy': b''

Now I have no idea where "copy" is.


The is a request for that exception to give that infomartion.
msg413789 - (view) Author: Miro Hrončok (hroncok) * Date: 2022-02-23 12:49
Apparently, the exception already contains a path attribute with exactly the kind of information I'd like to see.

What if the message was:

ImportError: bad magic number in '/usr/bin/six.pyc': b'\x03\xf3\r\n'

Would that be accepted as a PR? Should I discuss this on the mailing list?
msg413815 - (view) Author: Petr Viktorin (petr.viktorin) * (Python committer) Date: 2022-02-23 15:58
I assume a PR review should be enough.
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 90991
2022-02-24 20:09:00brett.cannonsetnosy: + brett.cannon
2022-02-23 17:48:42hroncoksetkeywords: + patch
stage: patch review
pull_requests: + pull_request29658
2022-02-23 15:58:08petr.viktorinsetmessages: + msg413815
components: + Library (Lib), - Interpreter Core
2022-02-23 12:49:33hroncoksetmessages: + msg413789
2022-02-23 12:43:36hroncokcreate