Pyrhon 3文件共享变量

时间:2017-08-29 12:56:43

标签: python-3.x function

我想从另一个python文件中导入一个var。

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

File1.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 

File2.py

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

错误:NameError:未定义名称'onlinecheckvarr'。

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

1 个答案:

答案 0 :(得分:-1)

可以通过以下方式完成:

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