[New-bugs-announce] [issue43969] "bad magic number" when Python 2's pyc file exists without py file

Mitchell Hentges report at bugs.python.org
Wed Apr 28 16:36:52 EDT 2021


New submission from Mitchell Hentges <mitch9654 at gmail.com>:

Python 3 imports may fail if a Python 2 .pyc file exists without an accompanying .py file.

-----

My project vendors code, and we recently updated "requests" to a drastically newer version.

As part of this version change, `requests/packages/__init__.py` was removed, and  `requests/packages.py` was added.

This project is long-lived, and many people have imported `requests` with Python 2. So, `requests/packages/__init__.pyc` exists.

After making this update, importing requests with Python 3 fails:
`ImportError: bad magic number in 'parent.child': b'\x03\xf3\r\n'`

Interestingly, deleting `requests/packages/__init__.pyc` allows Python 3 to work again.

-----

I've attached a "reproduction" script that produces the directory structure and tweak required to cause the failure.
I'm running Python 2.7.18 and Python 3.9.1.

----------
components: Interpreter Core
files: repro.sh
messages: 392254
nosy: mitchhentges
priority: normal
severity: normal
status: open
title: "bad magic number" when Python 2's pyc file exists without py file
type: behavior
versions: Python 3.6, Python 3.7, Python 3.8, Python 3.9
Added file: https://bugs.python.org/file49997/repro.sh

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43969>
_______________________________________


More information about the New-bugs-announce mailing list