简体   繁体   English

从脚本里面实现与python -mpdb相同的任何方式?

[英]Any way of achieving the same thing as python -mpdb from inside the script?

Besides wrapping all your code in try except , is there any way of achieving the same thing as running your script like python -mpdb script ? 除了将所有代码包装在try except ,是否有任何方法可以实现与运行脚本相同的功能,如python -mpdb script I'd like to be able to see what went wrong when an exception gets raised. 我希望能够在发生异常时看到出了什么问题。

If you do not want to modify the source then yOu could run it from ipython - an enhanced interactive python shell. 如果您不想修改源代码,那么yOu可以从ipython运行它 - 一个增强的交互式python shell。

eg run ipython then execute %pdb on to enable post-mortem debugging. 例如,运行ipython然后执行%pdb on以启用事后调试。 %run scriptname will then run the script and automatically enter the debugger on any uncaught exceptions. 然后, %run scriptname将运行该脚本并自动在任何未捕获的异常上输入调试器。

Alternatively %run -d scriptname will start the script in the debugger. 或者%run -d scriptname将在调试器中启动脚本。

python -i script

will leave you in the interactive shell when an exception gets raised; 当异常被引发时,你会留在交互式shell中; then 然后

import pdb
pdb.pm()

will put you into the post-mortem debugger so you can do all the usual debugging things. 将你带入事后调试器,这样你就可以做所有常用的调试工作。

This should work as long as your script does not call sys.exit. 只要您的脚本不调用sys.exit,这应该可以工作。 (Which scripts should never do, because it breaks this very useful technique! as well as making them harder to write tests for.) (哪些脚本永远不应该做,因为它打破了这个非常有用的技术!以及使它们更难为其编写测试。)

import pdb; pdb.set_trace()

资料来源: http//docs.python.org/library/pdb.html

暂无
暂无

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

相关问题 有没有办法在 python 中命名 function 和 class 相同的东西? - Is there a way to name a function and class the same thing in python? 有什么方法可以在 shell 脚本中导入 python 文件并使用 python 文件中 Z2591C18B701124B5EE24E 中的常量? - Is there any way to import a python file inside a shell script and use the constants present in python file inside shell script? 如何编写与此邮递员请求执行相同操作的python脚本 - How to write a python script that does the same thing as this postman request 是否可以从同一脚本中永久更改Python脚本中的变量? - Is it possible to permanently change a variable inside a Python script from the same script? 有没有更好的方法可以在Pandas中获得相似的结果或在Python中使用任何其他库 - Is there a better way in achieving similar results in Pandas or using any other library in Python 在python中为同一件事测试多个函数的更好方法 - Better way to test several functions for the same thing in python 有什么方法可以从网页运行内部 python 脚本? - Any way to run an internal python script from a webpage? 有什么办法可以使用python从zip内部提供用户上传的图片? - Any way to serve user uploaded images from inside zips with python? 有没有办法在python脚本中创建一个理解(来自Scitools)数据库? - Is there any way to create an Understand (from Scitools) DB within a python script? 有没有办法从 web 浏览器执行 Python 脚本? [等候接听] - Is there any way of executing a Python script from a web browser? [on hold]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM