[Python-ideas] Importing orphaned bytecode files (was: disabling .pyc and .pyo files)

Ben Finney ben+python at benfinney.id.au
Tue Dec 8 22:44:01 CET 2009

Kristján Valur Jónsson
<kristjan at ccpgames.com> writes:

> Repeatedly we run into trouble because of stray .pyo (and/or .pyc)
> files. This can happen for a variety of reasons, but most often it
> occurs when .py files are being removed, or moved in the hierarchy.
> The problem is that the application will happily load and import an
> orphaned .pyo file, even though the .py file has gone or moved.

Yes, I think Python users would benefit from having the above behaviour
be opt-in.

I suggest:

* A new attribute ‘sys.import_orphaned_bytecode’. If set ‘True’, the
  interpreter follows the current behaviour. If ‘False’, any bytecode
  file satisfies an import only if it has a corresponding source file
  (where “corresponding” means “this source file would, if compiled,
  result in a bytecode file replacing this one”).

  I suggest this attribute should be implemented as ‘True’ by default
  (to match current behaviour), then switched to ‘False’ by default as
  soon as feasible.

* The ‘PYTHONIMPORTORPHANEDBYTECODE’ environment variable, when set,
  causes the interpreter to set the above option ‘True’.

* The ‘-b’ option to the interpreter command-line sets the above option

 \         “I have yet to see any problem, however complicated, which, |
  `\      when you looked at it in the right way, did not become still |
_o__)                                more complicated.” —Paul Anderson |
Ben Finney

More information about the Python-ideas mailing list