如何从Python模块调用在主上下文中定义的函数?

时间:2018-10-21 16:01:06

标签: python function main

我想编写一个Python模块,该模块调用在主上下文中定义的函数。具体来说,这是我的“ qutest.py”模块伪代码(调用__main__.on_setup()无法编译):

def test(title):
    try:
        __main__.on_setup() # <== call on_setup() from the main scope
    except:
        print("on_setup not found")

~ ~ ~

这是我的主要模块:

from qutest import *

def on_setup():
    print("on_setup")

test("My first test")
~ ~ ~

目标是test()调用将调用在 main 上下文中定义的on_setup()。如何在我的qutest.py模块中进行编码?

非常感谢您的帮助!

0 个答案:

没有答案