Can't load bytecode with execfile ?

Nick Coghlan ncoghlan at
Tue Jan 25 22:49:51 EST 2005

Bo Jacobsen wrote:
> I have a number of files compiled to bytecode using py_compile.compile().
> The .pyc files can be invoked by python directly ($python file.pyc), but
> "loading" them by execfile just throws an exception ?

Py> help(execfile)
Help on built-in function execfile in module __builtin__:

     execfile(filename[, globals[, locals]])

     Read and execute a Python _SCRIPT_ from a file.
     The globals and locals are dictionaries, defaulting to the current
     globals and locals.  If only globals is given, locals defaults to it.

(Emphasis Added)

Execfile doesn't go through the "is it bytecode?" check that the import 
machinery or the -m command line switch use. It tries to interpret anything you 
give it as Python source code - which is why you get a syntax error when you try 
to use it on compiled bytecode.

Allowing it to handle compiled bytecode might make a reasonable feature request, 


Nick Coghlan   |   ncoghlan at   |   Brisbane, Australia

More information about the Python-list mailing list