我想从另一个 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")
关于python-3.x - Python 3 文件共享变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45939782/