NameError using execfile in python

My application has a button for dynamically executing a python script using execfile. If I define a function inside a script (e.g. spam ()) and try to use this function inside another function (e.g. egg ()), I get this error:

NameError: global name 'spam' is not defined

What is the correct way to call spam () from inside egg ()?

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

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

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

def eggs():
    spam()

eggs()

In addition, I cannot execute the method from my main application in a script. i.e.

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

 #my2ndscript.py
 bleh()

Mistake:

NameError: name 'bleh' is not defined

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

EDIT : updated first issue

+3
source share
4 answers

import ( , "mainprogram.py" $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()

mainprogram. :

#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()

, execfile . import __import__ ( reload() , script ).

#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()
+2

3 8 , , , , , ( , , , ), .

[]

, , . , .

Python execfile() builtin. , Python 3.x.

execfile() runme(), spam() eggs() runme(), ( ). :

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()

>>>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>}

, spam() , runme(). , , spam()

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

spam(), runme(). , spam() :

myscript.py

global spam
def spam():
    print "spam"

def eggs():
    spam()

eggs()

spam() globals() (.. ), eggs().

+10

Addy689 : execfile() , . execfile() . " ".

. , , exec() ( : fooobar.com/questions/167607/...). execfile()

def callingFunction(filename)
    # ... 
    d = dict(locals(), **globals())
    execfile(filename, d, d )

, script: , if name == main, ,

+6

, , ? script .

: "bleh" , "mainprogram"

+1
source

Source: https://habr.com/ru/post/1709327/


All Articles