Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
849 views
in Technique[技术] by (71.8m points)

class - NameError using execfile in python

My application has a button to execute a python script dynamically using execfile. If I define a function inside the script (eg. spam()) and try to use that function inside another function (eg. eggs()), I get this error:

NameError: global name 'spam' is not defined

What is the correct way to call the spam() function from within eggs()?

#mainprogram.py
class mainprogram():
    def runme(self):
        execfile("myscript.py")

>>> this = mainprogram()
>>> this.runme()

# myscript.py
def spam():
    print "spam"

def eggs():
    spam()

eggs()

Also, I can't seem to be able to execute a method from my main application in the script. i.e.

#mainprogram.py
class mainprogram():
    def on_cmdRunScript_mouseClick( self, event ):
        execfile("my2ndscript.py")
    def bleh():
        print "bleh"

 #my2ndscript.py
 bleh()

The error is:

NameError: name 'bleh' is not defined

What is the correct way to call bleh() from my2ndscript.py?

EDIT: Updated first issue

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're 3 years 8 months wiser since you posted so I'm assuming you'd have figured the first issue out, but given that a solution has not yet been posted (primarily because no one seemed to have a problem with the first issue), the following is my solution.

[UPDATED]

The last solution I provided was incorrect. Below I am providing the correct solution and explaining it in detail using code that I executed.

The problem is inherent in Python's execfile() builtin. Its one of the reasons that function has been deprecated in Python 3.x.

When you executed execfile() inside runme(), the objects spam() and eggs() were loaded into method runme()'s namespace, and not into the global namespace (as they should ideally be). Consider the following code:

myscript.py

def spam():
    print 'spam'

def eggs():
    if 'spam' not in globals():
        print 'method spam() is not present in global namespace'
    spam()

try:
    eggs()
except Exception as e:
    print e

mainprogram.py

class mainprogram():
    def runme(self):
        execfile("myscript.py")
        print 'Objects lying in local namespace of runme() are -'
        print locals()

this = mainprogram()
this.runme()

Interpreter Output

>>>import mainprogram
method spam() is not present in global namespace
name 'spam' is not defined
Objects lying in local namespace of runme() are -
{'e': NameError("name 'spam' is not defined",), 'spam': <function spam at 0x000000000000002B>, 'eggs': <function eggs at 0x000000000000002C>, 'self': <mainprogram.mainprogram instance at 0x000000000000002D>}

From the output you can see that spam() is not in the global namespace, but in method runme()'s namespace. So hypothetically, the correct way to call spam() would have been

def eggs():
    global this
    this.runme.spam()

However, there is no way to access spam() while it is lying inside runme()'s namespace. The solution, therefore, is to insert spam() in the global namespace as follows:

myscript.py

global spam
def spam():
    print "spam"

def eggs():
    spam()

eggs()

This will ensure that a reference to object spam() is created inside the globals() dictionary (i.e., the global namespace), making it callable from eggs().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...