REPOST: (Almost) command line simulation with execfile

Stefan Schwarzer s.schwarzer at ndh.net
Fri Dec 28 12:36:40 EST 2001


Hello

I'm trying to make a wrapper class to execute example scripts. For
that, I use

class Example:
    '''Provide demonstration services for Python scripts.'''

    ...

    def execute(self):
        '''Read and execute the example script.'''
        print 'Executing %s ...' % self.filename
        print
        temp_globals = globals().copy()
        temp_locals = {}
        try:
            execfile(self.filename, temp_globals, temp_locals)
        except:
            print "Ooops, there seems to be a problem ..."
            print
            traceback.print_exc(file=sys.stdout)

If it wasn't for catching exceptions in the example scripts caused by
invalid user input, I would use os.system. On the other hand, the
scripts should be simple, so I would not like to put the exception
handling stuff in (every) example script.

My problem is that the above execfile call doesn't behave similar to
os.system if I have import statements in the executed file. In this
case, I get a name error which I do not get with  python file.py
(i. e. executing the example script directly from the command line).

The output of a wrapped script with an  import urllib  statement is

-----
Executing internet.py ...

Please ensure you are connected to the internet and press [Return]:

The first 150 characters from http://www.python.org/:
Ooops, there seems to be a problem ...

Traceback (most recent call last):
  File "wrapper.py", line 80, in execute
    execfile(self.filename, temp_globals, temp_locals)
  File "internet.py", line 50, in ?
    print url_content_part(url, count)
  File "internet.py", line 12, in url_content_part
    fetched = urllib.urlopen(url)
NameError: global name 'urllib' is not defined
-----

Perhaps it's a problem with the globals dictionary? What should I
probably use instead? (Note that, in contrast to other postings I have
found with Google groups, I do _not_ want to have modified the
namespaces of my wrapper module or the class instance.)

Can somebody explain what's going on here and what I should do? :-)
Many thanks in advance!

Stefan

========= WAS CANCELLED BY =======:
Path: news.sol.net!spool0-milwwi.newsops.execpc.com!newsfeeds.sol.net!news-out.visi.com!hermes.visi.com!newsfeed.direct.ca!look.ca!nntp.kreonet.re.kr!feeder.kornet.net!news1.kornet.net!ua4canc3ll3r
From: Stefan Schwarzer <s.schwarzer at ndh.net>
Newsgroups: comp.lang.python
Subject: cmsg cancel <3C2CADA8.8D6BE4C at ndh.net>
Control: cancel <3C2CADA8.8D6BE4C at ndh.net>
Date: Mon, 31 Dec 2001 03:21:37 GMT
Organization: A poorly-installed InterNetNews site
Lines: 2
Message-ID: <cancel.3C2CADA8.8D6BE4C at ndh.net>
NNTP-Posting-Host: 211.57.49.2
X-Trace: news2.kornet.net 1009775196 27193 211.57.49.2 (31 Dec 2001 05:06:36 GMT)
X-Complaints-To: usenet at news2.kornet.net
NNTP-Posting-Date: Mon, 31 Dec 2001 05:06:36 +0000 (UTC)
X-No-Archive: yes
X-Unac4ncel: yes
X-Commentary: I love NewsAgent 1.10 and the Sandblaster Cancel Engine Build 74 (19 March 1999)

This message was cancelled from within Mozilla.



More information about the Python-list mailing list