"列出索引超出范围" python exec()上的警告

时间:2018-02-17 15:56:32

标签: python ide

我正在使用Python 3(3.5.3)在Thonny中调试python脚本。我在Raspberry Pi ZeroW上使用Linux raspberrypi 4.9.59+#1047运行它。我通过Wi-Fi通过realVNC远程连接。

Python脚本有一些初始化代码,可以将一些变量设置为默认值。该脚本按需运行;没有错误。

我正在添加从文件中读取一组配置参数以初始化变量的功能。

config = {}
config_dir = os.path.dirname(os.path.abspath(__file__))
config_file = config_dir + "/temps.conf"
with open(config_file, "r") as fh:
    exec(fh.read()+"\n", config)
    fh.close()

只需开始调试脚本(即,不是单行代码解释,只需ctrl + F5)会导致回溯:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/thonny/shared/thonny/ast_utils.py", line 178, in _mark_text_ranges_rec
    tokens = _mark_end_and_return_child_tokens(node, tokens, prelim_end_lineno, prelim_end_col_offset)
  File "/usr/lib/python3/dist-packages/thonny/shared/thonny/ast_utils.py", line 263, in _mark_end_and_return_child_tokens
    _strip_trailing_junk_from_expressions(tokens)
  File "/usr/lib/python3/dist-packages/thonny/shared/thonny/ast_utils.py", line 202, in _strip_trailing_junk_from_expressions
    while (tokens[-1].type not in (token.RBRACE, token.RPAR, token.RSQB,
"IndexError: list index out of range"

通过注释掉新的代码块并一次取消注释一个语句,我已经将导致错误的语句缩小到" exec(...)"线。同样,错误发生在任何语句运行之前。虽然发生了错误,但Thonny允许我继续执行脚本。如果我单步执行该程序,我期待exec语句失败。实际上,代码执行得很好。读取配置文件并将其加载到我的配置数组中。然后我可以在我的Python脚本的其余部分中检索参数。

因此,看起来这个回溯更多的是警告而不是严重错误。我可以忽略它,但我希望这是试图告诉我一些事情 - 哪里有烟,哪里可能是闷烧的火。我想了解造成这个问题的原因,以便我可以消除它。

感谢您的见解!

UPDATE - temps.conf文件的内容

# Configuration for temperature logging scripts.
#
# Create a copy of this file, adjust it to suit your needs, and save the copy as
# `temps.conf` in the same directory.

#
# Local Temperature sensor settings.
#

# Calibration for local temperature sensor, in degrees F (e.g. 3 -2, -1.24).
# Average variance in temperature readings between Nest thermostat and Pimoroni enviroPhat
local_temp_offset = 8.95

# Temperature difference trigger threshold, in degrees F.
temp_threshold = 2

# Amount of time to wait before checking temperature after adjustment, in seconds.
wait_to_temp = 300

# Looping cycle delay between local temperature readings when just monitoring temperature, in seconds.
local_temp_read_delay = 30

# Open Weather Maps API Key
owm_api_key = 'OWN_API_KEY'

# Nest - Cannot set target temperatures closer than 3.0 degrees F 
nest_heat_cool_spread = 3

# Nest Developers Cloud API parameters.
nest_client_id = 'NEST_CLIENT_ID'
nest_client_secret = 'NEST_CLIENT_SECRET'
nest_access_token_cache_file = '/home/pi/.config/nest/token_cache'

麦克

1 个答案:

答案 0 :(得分:0)

这确实是Thonny的一个错误。我创建了一个问题:https://bitbucket.org/plas/thonny/issues/396/exec-causes-range-marker-to-crash