469,934 Members | 2,227 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

Can't load bytecode with execfile ?

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.
Jul 18 '05 #1
1 2898
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(...)
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.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Enrique | last post: by
4 posts views Thread by Roman Suzi | last post: by
reply views Thread by Enrique Palomo Jiménez | last post: by
10 posts views Thread by leonel.gayard | last post: by
5 posts views Thread by George Sakkis | last post: by
2 posts views Thread by Igor Kaplan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.