2011-06-17 28 views
1

是否可以顯示行號和警告?我收到一些警告,可能來自numpy,但我不知道它們來自哪裏。我不想讓我的代碼登上或者發出異常,但是我想從警告的起源中獲得更多信息。這有可能嗎?如何知道警告來自Python的位置

回答

1

警告是通過默認打印包括文件名和行號,和輸出可以通過warnings模塊中的功能,以及由-W參數Python解釋來控制。由於您的警告顯然不包含文件名和行號,因此warnings模塊可能無法幫助您。既然你懷疑numpy可能是罪魁禍首,我建議看看numpy.seterr()函數。也許把警告轉化爲錯誤有幫助。

0

編輯:

精氨酸!我莫名其妙地弄錯了名字。

使用pychecker模塊。如果你安裝了distutils,只需在命令行輸入:easy_install pychecker即可獲得最新版本。默認情況下會生成警告,並列出它們的行號。

+0

@Charles Brunet對不起。我混合了我的模塊的名稱。它實際上是pychecker。如果有必要,它可以進入你的被調用的模塊,所以它可以檢查你的numpy。 –

+0

pychecker分析源代碼,它對運行時錯誤和警告沒有任何作用。 –

1

編寫一個像這樣的單獨模塊。其經由warnings模塊發出

import warnings 

# capture all warnings 
with warnings.catch_warnings(record=True) as warns: 
    warnings.simplefilter("always") 
    # do the stuff that triggers warnings. i.e. import your main module 
    # and call whatever is necessary to get it going. This must all be 
    # indented under the with statement! 

# afterward, print captured warnings 
for w in warns: 
    print w.category.__name__, "(%s)" % w.message, 
    print "in", w.filename, "at line", w.lineno