NameError在python中使用execfile [英] NameError using execfile in python

查看:172
本文介绍了NameError在python中使用execfile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个按钮,可以使用 execfile 动态地执行python脚本。如果我在脚本中定义了一个函数(例如。 spam()),并尝试在另一个函数中使用该函数:

  NameError:未定义全局名称spam未定义

eggs()中调用 spam()函数的正确方法是什么?

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

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

#myscript.py
def spam():
printspam

def eggs b spam()

egg()

似乎能够从我的主应用程序在脚本中执行一个方法。

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

#my2ndscript.py
bleh $ b

错误是:

  NameError:name'bleh'未定义

EDIT / p>

解决方案

在第二种情况下,您将需要 import mainprogram.py
在您的 $ PYTHONPATH

 #mainprogram.py 
class mainprogram:
def runme(self):
execfile(my2ndscript.py)
def bleh(self):
print如果__name__ =='__main__':
mainprogram()。runme()

#my2ndscript.py
import mainprogram
x = mainprogram。 mainprogram()
x.bleh()

c $ c> mainprogram 。或者更好:

 #mainprogram.py 
class mainprogram:
def runme b $ b execfile(my2ndscript.py,globals = {'this':self})
def bleh(self):
printbleh
if __name__ =='__main__' :
mainprogram()。runme()

#my2ndscript.py
this.bleh()

我猜想 execfile 对你的问题不是正确的解决方案。
为什么不使用 import __ import __ (和 reload ,如果脚本可能会在这些点击之间变化)。

 #mainprogram.py 
import my2ndscript

class mainprogram:
def runme(self):
reload(my2ndscript)
my2ndscript.main(self)
def bleh
printbleh

如果__name__ =='__main__':
mainprogram()。runme()

#my2ndscript.py
def main(程序):
program.bleh()


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

解决方案

In the second case you will need import (not sure whether "mainprogram.py" is on your $PYTHONPATH)

#mainprogram.py
class mainprogram:
    def runme(self):
        execfile("my2ndscript.py")
    def bleh(self):
        print "bleh"
if __name__ == '__main__':
    mainprogram().runme()

#my2ndscript.py
import mainprogram
x = mainprogram.mainprogram()
x.bleh()

but this will craete second instance of mainprogram. Or better yet:

#mainprogram.py
class mainprogram:
    def runme(self):
        execfile("my2ndscript.py", globals={'this': self})
    def bleh(self):
        print "bleh"
if __name__ == '__main__':
    mainprogram().runme()

#my2ndscript.py
this.bleh()

I guess that execfile is not right solution for your problem anyway. Why don't you use import or __import__ (and reload() in case script may change between those clicks).

#mainprogram.py
import my2ndscript

class mainprogram:
    def runme(self):
        reload(my2ndscript)
        my2ndscript.main(self)
    def bleh(self):
        print "bleh"

if __name__ == '__main__':
    mainprogram().runme()

#my2ndscript.py
def main(program):
    program.bleh()

这篇关于NameError在python中使用execfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆