显示“未解析引用”的全局范围变量

2024-04-19 06:20:26 发布

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

我有以下代码:

import json
import os
from botocore.vendored import requests
from custom_validator import CustomValidator

dev_mode = 'DEV' in os.environ
access_token = None

if dev_mode:
    import test_config as config
else:
    import config as config

def myfunc(data):
     if not (access_token):
         .....

但是在myfunc内部,我在尝试使用access_token时得到了Unresolved reference错误。 即使我在上面的初始化中把global放在它前面,我也会得到:

Global variable 'access_token' is undefined in the module level

我有点困惑-为什么会发生这种情况,我该怎么解决?你知道吗

使用python3.6。你知道吗


Tags: 代码infromdevimporttokenconfigjson