2013-03-14 203 views
0

我已经得到类似这样的错误最近:神秘IndexError

IndexError        Traceback (most recent call last) 
<ipython-input-124-59ca523b1b36> in <module>() 
----> 1 first_experiment_comb(model) 

    c:\python26\26664\lib\site-packages\experiments.py in first_experiment_comb(mod 
l) 
    172      "Number NZ: " + str(modelz[j].NumNZs) +"\n") 
    173 
--> 174   first_experiment(modelz[j], str(j)) 
    175 
    176 

c:\python26\26664\lib\site-packages\experiments.py in first_experiment(model, e 
t) 
    89   plt.close() 
    90 
---> 91  fl.timberFlow(model) 
    92  plt.savefig(dire + "\\timber_flow" +ext+".pdf", bbox_inches = 0) 
    93  plt.close() 

C:\Python26\26664\lib\site-packages\func_lib.py in timberFlow(model) 
    304  if not unVars: 
    305   unVars = varValues(model, 'PIEHTLVOL') 
--> 306 
    307  for i in range(19): 
    308    swVarVals.append(swVars[i].X) 

IndexError: list index out of range 

其中跟踪点,代码不存在,或者在以往的案例的最后一行已被注释掉。当我自己运行最后一个函数(在func_lib.py中)时,我从来没有得到神秘的IndexError,只有当它从experiments.py中调用时。

我在python python 2.6 W64中运行这个。 我一直无法在iPython或Pylab文档中发现有关此的已知错误。

306行怎么会成为错误的根?

+0

您的代码与字节码不同步。正确地重新加载你的代码。 – 2013-03-14 21:43:13

+0

无论何时编辑包中的文件,我都会看到这一点,但不要重新编译。也许有更正确的方式来重新加载代码,但我做的是,删除'func_lib.pyc',然后重新启动python并重新导入模块这是从 – 2013-03-14 21:45:29

回答

3

您的代码与字节码不同步。正确地重新加载你的代码。

当发生异常时,将检查字节码中的文件名和绣线编号,然后加载源文件以显示该线的原始源。

但是,如果您更改了源代码但尚未重新启动您的python进程(或重新加载了ipython中的代码),那么当发生异常时将显示错误的行。

+0

我一直使用%ed然后保存代码并重新运行。这是否不正确地重新加载代码? – ABM 2013-03-14 21:50:10

+0

并不总是;有警告。请参阅[ipython autoreload文档](http://ipython.org/ipython-doc/stable/config/extensions/autoreload.html),其中列出了一些。 – 2013-03-14 21:58:18

+1

经验法则:当你的回溯不再有意义时,你需要重新启动ipython。 :-) – 2013-03-14 21:58:34