| Store | Cart

execfile() on file subclass or string

From: Fredrik Lundh <fred...@pythonware.com>
Thu, 24 Mar 2005 14:36:14 +0100
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


Recent Messages in this Thread
Brano Zarnovican Mar 24, 2005 09:19 am
Do Re Mi chel La Si Do Mar 24, 2005 12:40 pm
Fredrik Lundh Mar 24, 2005 01:36 pm
Brano Zarnovican Mar 24, 2005 03:05 pm
Messages in this thread