execfile() on file subclass or string

Fredrik Lundh fredrik at pythonware.com
Thu Mar 24 14:36:14 CET 2005


Brano Zarnovican wrote:

> I have a python script represented by a string.
> I need to execute it in a context. 'exec' does
> the job, but doesn't display the filename in
> tracebacks. 'execfile' is displaying the filename
> but it can only exec a script in a filesystem.
>
> I have tried:
> - to give exec a filename, like:
>
>  exec script_content in dict({'__file__':'bla.py'})
>
>  => didn't work (neighter with __name__)

compile the script first, and execute the resulting code object:

>>> exec compile("code", "filename", "exec")
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "filename", line 1, in ?
NameError: name 'code' is not defined

</F> 






More information about the Python-list mailing list