如何从导入的库中执行函数?

2024-09-30 14:24:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在编写一个Python程序,但是Python的语法对我来说还是比较新的。例如,如果我有两个包含以下内容的Python文件,那么执行函数的正确方法是什么

main.py-这是正在执行的文件

#!/usr/bin/env python

#threading array
threads = []

import threading
import anotherFile as af

afCheck = af.init() #return true or false
if afCheck == True: 
    threads.append(threading.Thread(target=af.listener))

try:
    for thread in threads:
        thread.start()
except (KeyboardInterrupt, SystemExit):
    print("closing up shop");
    for thread in threads:
        thread.cancel()
    sys.exit()

我本来打算为anotherFile.py编写一些伪代码,但这似乎无关紧要。我已经能够测试到我看到它没有执行侦听器的程度,并且我不确定调用它的正确方法

也许我只需要在anotherFile.py中插入侦听器,但是为了简单起见,最好将它们都放在一个地方,因为这将有多个导入


Tags: 文件方法inpyimport程序for语法