Disable use of pyc file with no matching py file

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Jan 31 07:01:35 EST 2012


On Tue, 31 Jan 2012 11:26:10 +0100, Jean-Michel Pichavant wrote:

> Terry Reedy 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.
>>
> No.


Is that intended as "No, I won't upgrade" or "No, Python 3.2 doesn't do 
the job"?

If the first one, then that's your decision, of course, but there really 
is no other straight-forward way to have Python ignore left-over .pyc 
files. Like it or not, there is no way to disable the use of .pyc files 
in older versions of Python. 

(I suppose you could write a patch for Python, and use your custom 
version, but that's it.)

The ability to use .pyc files without providing source code is considered 
by some people a feature, not a bug, and in fact even in Python 3.2 the 
same applies. The difference in 3.2 is that you can't *accidentally* use 
old left-over .pyc files, you have to move and rename them before they 
can be used as sourceless modules.



-- 
Steven



More information about the Python-list mailing list