Python 3 exec method : NameError: name of a defined function is not defined
Python 3 exec method : NameError: name of a defined function is not defined
工人档案:vir.py
def calcCycleOffset():
global cycleOffset, uc, cs
cycleOffset = uc - cs
return cycleOffset
def vir_main():
calcCycleOffset()
vir_main()
#calcCycleOffset()
print( "vir.py: cycleOffset: ", cycleOffset )
执行器文件:exec.py
filename = "./vir.py"
vir_globals = { "uc": 42, "cs": 12, "cycleOffset": -1 }
with open(filename, "rb") as source_file:
code = compile(source_file.read(), filename, "exec")
exec(code, vir_globals, {} )
print( 'exec: globals.cycleOffset', vir_globals['cycleOffset'] )
此模式失败:
Traceback (most recent call last):
File "exec.py", line 7, in <module>
exec(code, vir_globals, {} )
File "./vir.py", line 12, in <module>
vir_main()
File "./vir.py", line 9, in vir_main
calcCycleOffset()
NameError: name 'calcCycleOffset' is not defined
但是,python 知道函数 calcCycleOffset()。如果您将 vir.py 中的评论从:
切换为
vir_main()
#calcCycleOffset()
至:
#vir_main()
calcCycleOffset()
那么函数调用就OK了
我希望能够在 vir.py 文件的函数中嵌套函数,并且仍然能够动态地执行它...
求助!
谢谢
如官方文档中关于 locals 对象的解释:https://docs.python.org/3.8/library/functions.html#locals
Note : The contents of this dictionary should not be modified; changes
may not affect the values of local and free variables used by the
interpreter.
并且您作为本地对象向此处传递了一个空对象。
在您的执行程序文件中请将您的代码更改为:
filename = "./vir.py"
vir_globals = { "uc": 42, "cs": 12, "cycleOffset": -1 }
with open(filename, "rb") as source_file:
code = compile(source_file.read(), filename, "exec")
exec(code, vir_globals) #remove the empty object here !!
print( 'exec: globals.cycleOffset', vir_globals['cycleOffset'] )
它应该是技巧
你也可以看看这个问题和@thefourtheye 的精彩回答
Using a function defined in an exec'ed string in Python 3
工人档案:vir.py
def calcCycleOffset():
global cycleOffset, uc, cs
cycleOffset = uc - cs
return cycleOffset
def vir_main():
calcCycleOffset()
vir_main()
#calcCycleOffset()
print( "vir.py: cycleOffset: ", cycleOffset )
执行器文件:exec.py
filename = "./vir.py"
vir_globals = { "uc": 42, "cs": 12, "cycleOffset": -1 }
with open(filename, "rb") as source_file:
code = compile(source_file.read(), filename, "exec")
exec(code, vir_globals, {} )
print( 'exec: globals.cycleOffset', vir_globals['cycleOffset'] )
此模式失败:
Traceback (most recent call last):
File "exec.py", line 7, in <module>
exec(code, vir_globals, {} )
File "./vir.py", line 12, in <module>
vir_main()
File "./vir.py", line 9, in vir_main
calcCycleOffset()
NameError: name 'calcCycleOffset' is not defined
但是,python 知道函数 calcCycleOffset()。如果您将 vir.py 中的评论从:
切换为vir_main()
#calcCycleOffset()
至:
#vir_main()
calcCycleOffset()
那么函数调用就OK了
我希望能够在 vir.py 文件的函数中嵌套函数,并且仍然能够动态地执行它...
求助!
谢谢
如官方文档中关于 locals 对象的解释:https://docs.python.org/3.8/library/functions.html#locals
Note : The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter.
并且您作为本地对象向此处传递了一个空对象。
在您的执行程序文件中请将您的代码更改为:
filename = "./vir.py"
vir_globals = { "uc": 42, "cs": 12, "cycleOffset": -1 }
with open(filename, "rb") as source_file:
code = compile(source_file.read(), filename, "exec")
exec(code, vir_globals) #remove the empty object here !!
print( 'exec: globals.cycleOffset', vir_globals['cycleOffset'] )
它应该是技巧
你也可以看看这个问题和@thefourtheye 的精彩回答 Using a function defined in an exec'ed string in Python 3