python-3.x - Python 3 文件共享变量

我想从另一个 python 文件导入一个变量。

第一个文件在第二个窗口的 file2 旁边运行。

文件1.py

def onlinecheckext():
global onlinecheckvarr
while 1:
    try:
        onlinecheckscc = pyautogui.locateOnScreen(imPath("start_detect.png"))
        if onlinecheckscc is None:
            logging.debug("I am Online")
            onlinecheckvarr = True
            time.sleep(2)
        else:
            logging.debug("I am not online")
            onlinecheckvarr = False
            time.sleep(2)


    except:
        logging.debug("Online check fail")
        return False 

文件2.py

from file1 import onlinecheckvarr
while onlinecheckvarr is True:
    print("TEST")
else:
    print("Test2")

错误:NameError:未定义名称“onlinecheckvarr”。

我已经尝试了几乎所有的方法,但仍然没有得到它,它是一个全局变量,我认为我正确地导入了它,但它仍然会抛出错误。

最佳答案

可以这样做:

from file1 import onlinecheckext # <--
while onlinecheckvarr is True:
    print("TEST")
else:
    print("Test2")

https://stackoverflow.com/questions/45939782/

相关文章:

selenium - 使用 SendKeys 向 IE 11 发送 '@' 特殊字符

javascript - 如何将样式(css)和js添加到 Handlebars 文件?

apache-spark - Kafka 结构化流 java.lang.NoClassDefFoun

java - 反编译MCP报错(9.18版)返回 'Decompile Failed' 1

google-cloud-platform - Google Cloud Endpoints 以 V

string - 如何删除大括号之间的文本

c# - Signalr net client On 事件泛型类型参数

php - 找不到属性 Application\Sonata\NewsBundle\Entity\P

azure - Azure WebApp 是否自动进行速率限制/DOS 保护?

r - 无法删除文件 - 打开我的 Rstudio