Python stupidity of the day
Feb. 8th, 2011 11:11 amPython compiles source files to byte code automatically whenever a module is imported; to save itself time, it does this only if the bytecode file (.pyc) does not exist or has an mtime older than the corresponding source file.
Reasonable enough so far; now here's the stupid: if the corresponding source file no longer exists but the bytecode file has been left in place, python will silently use the bytecode. Suppose we have script.py and module.py in one directory, and another module.py elsewhere on the search path, so the local module.py overrides the other one. Now suppose, after this has been run at least once and module.pyc has been created in the local directory, module.py is deleted; in the work-related example I'm thinking of, module.py was a different implementation of uuid, and the silliness that motivated it eventually went away.
It turns out that, in this case, Python check if the mtime on the source file is newer than the mtime on the bytecode file, but fails to check for the continued existence of the source file, leading me to some mysterious exceptions, a wasted fifteen minutes of bafflement and a large *facepalm*.
![]()