Can't load bytecode with execfile ?

B

Bo Jacobsen

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 ?

Any suggestions

Bo.
 
N

Nick Coghlan

Bo said:
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(...)
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,
though.

Cheers,
Nick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top