简体   繁体   English

Python:如何找到为什么IDLE重新启动?

[英]Python: How do I find why IDLE restarts?

I am using python 2.5 on windows. 我在Windows上使用python 2.5。 All I am doing is unpickling a large file (18MB - a list of dictionaries) and modifiying some of its values. 我要做的只是解压缩一个大文件(18MB-词典列表)并修改其某些值。 Now this works fine. 现在可以正常工作了。 But when I add a couple of prints, IDLE restarts. 但是,当我添加几个打印件时,IDLE重新启动。 And weirdly enough it seems to be happening where I added the print. 而且奇怪的是,似乎在我添加印刷品的地方就发生了。 I figured this out commenting and uncommenting things line by line. 我想出了逐行注释和取消注释的方法。 I added a try catch around the print, but am not able to catch anything. 我在打印件周围添加了一个try catch,但无法捕获任何东西。 When does IDLE restart? IDLE何时重新启动? And how do I catch any exceptions or errors it throws(if it does)? 以及如何捕获它引发的任何异常或错误(如果有)?

Have you tried running your script from the command line rather than IDLE? 您是否尝试过从命令行而不是IDLE运行脚本? Open a command prompt and type python to enter the Python interpreter. 打开命令提示符,然后键入python进入Python解释器。 See if it crashes there too. 看看它是否也在那里崩溃了。

Secondly, you should try using the pdb module for debugging your Python scripts. 其次,您应该尝试使用pdb模块调试Python脚本。 This is far more effective than print statements since you can step through your code and check values at any point during the debug session. 这比print语句有效得多,因为您可以在调试会话期间的任何时候逐步执行代码并检查值。

import pdb

test_var = 'this is a test'

# set this whenever you want to start a breakpoint
pdb.set_trace()

In a pdb debug session you can step through lines by pressing 'n' and print values directly using the print statement. 在pdb调试会话中,您可以通过按'n'单步操作,并直接使用print语句打印值。 For example, you could: 例如,您可以:

> print test_var
'this is a test'

启用调试器,看看它是否告诉您任何信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM