Disable use of pyc file with no matching py file

John Roth johnroth1 at gmail.com
Wed Feb 1 14:11:05 CET 2012


On Jan 31, 4:43 pm, Terry Reedy <tjre... at udel.edu> wrote:
> On 1/31/2012 3:20 PM, John Roth wrote:
>
> > On Jan 30, 3:43 pm, Terry Reedy<tjre... at udel.edu>  wrote:
> >> On 1/30/2012 4:30 PM, Roy Smith wrote:
>
> >>> Every so often (typically when refactoring), I'll remove a .py file
> >>> and forget to remove the corresponding .pyc file.  If I then import
> >>> the module, python finds the orphaned .pyc and happily imports it.
> >>> Usually leading to confusing and hard to debug failures.
>
> >>> Is there some way to globally tell python, "Never import a .pyc
> >>> unless the corresponding .py file exits"?
>
> >> Upgrade to 3.2.
>
> I tested before writing this. The caveat is that x.pyc in the same
> directly as x.py will not be ignored (for back compatibility). However,
> this only happens intentionally as .pyc files are put in __pycache__/
> with name x.<version>.pyc, where <version> is 'cpython-32' or something
> similar for another version or implementation.
>
> > I've noticed that the tutorial (section 6.1.3) hasn't been updated for
> > PEP 3147; there's no way of telling that this is the behavior from
> > reading the tutorial. The development doc for 3.3 hasn't been updated
> > either.
>
> You are right. An oversight. Thanks for noticing.http://bugs.python.org/issue13915
> Suggested rewrites are welcome.
>
> --
> Terry Jan Reedy

I'll see if I can put a suggestion in the bug report.

One other point: I'm unclear if a compiled module in the source
directory would be named spam.pyc or spam.cpython-32.pyc. I'd think
the latter to allow two versions of a compiled-only distribution.

John Roth




More information about the Python-list mailing list