在 python 程序中解释 python 代码

Riv*_*ven 0 python pip pythoninterpreter

是否有用于在 python 程序中解释 python 代码的库?
示例用法可能如下所示..

code = """
def hello():
    return 'hello'

hello()
"""

output = Interpreter.run(code)
print(output)
Run Code Online (Sandbox Code Playgroud)

然后输出 hello

War*_*x56 5

从grepper找到这个例子

the_code = '''
a = 1
b = 2
return_me = a + b
'''

loc = {}
exec(the_code, globals(), loc)
return_workaround = loc['return_me']
print(return_workaround)
Run Code Online (Sandbox Code Playgroud)

显然你可以将全局和局部范围传递到exec. 在您的用例中,您只需使用命名变量而不是返回。