python - 如何将 pdb 注入(inject)失败的 Python 脚本?

标签 python django debugging pdb

我正在开发一个 django 项目,该项目有一个无法加载的大型装置:

$ python manage.py loaddata apps/mainsite/fixtures/test_auctions.json 
/Users/cp/bidsite/.ve/lib/python2.6/site-packages/django/db/models/fields/subclassing.py:80: DeprecationWarning: A Field class whose get_db_prep_save method hasn't been updated to take a `connection` argument.
  new_class = super(SubfieldBase, cls).__new__(cls, name, bases, attrs)
/Users/cp/bidsite/.ve/lib/python2.6/site-packages/django/db/models/fields/subclassing.py:80: DeprecationWarning: A Field class whose get_db_prep_lookup method hasn't been updated to take `connection` and `prepared` arguments.
  new_class = super(SubfieldBase, cls).__new__(cls, name, bases, attrs)
/Users/cp/bidsite/.ve/lib/python2.6/site-packages/celery/task/schedules.py:5: DeprecationWarning: celery.task.schedules is deprecated and renamed to celery.schedules
  "celery.task.schedules is deprecated and renamed to celery.schedules"))
Problem installing fixture 'apps/mainsite/fixtures/test_auctions.json': Traceback (most recent call last):
  File "/Users/cp/bidsite/.ve/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 174, in handle
    obj.save(using=using)
  File "/Users/cp/bidsite/.ve/lib/python2.6/site-packages/django/core/serializers/base.py", line 165, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "/Users/cp/bidsite/.ve/lib/python2.6/site-packages/django/db/models/base.py", line 570, in save_base
    created=(not record_exists), raw=raw, using=using)
  File "/Users/cp/bidsite/.ve/lib/python2.6/site-packages/django/dispatch/dispatcher.py", line 172, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/Users/cp/bidsite/apps/mainsite/models.py", line 257, in update_auction_details
    auction_json = instance.as_json()
  File "/Users/cp/bidsite/apps/mainsite/models.py", line 1110, in as_json
    'product': self.product.as_json(),
  File "/Users/cp/bidsite/.ve/lib/python2.6/site-packages/django/db/models/fields/related.py", line 315, in __get__
    rel_obj = QuerySet(self.field.rel.to).using(db).get(**params)
  File "/Users/cp/bidsite/.ve/lib/python2.6/site-packages/django/db/models/query.py", line 349, in get
    % self.model._meta.object_name)
DoesNotExist: Product matching query does not exist.

问题是堆栈跟踪没有给我任何关于设备上的哪一行导致此错误的线索。我该如何调试呢?我唯一能想到的是 ipython 中有一个功能,每当你执行某事并引发异常时,ipython 会自动注入(inject)一个 pdb 提示,这样你就可以四处看看发生了什么。我该怎么做呢?是否有用于 python 的命令行开关?我可以在这里做什么来调试它?

最佳答案

这个 Python Recipe 将安装一个调试器,该调试器在未捕获的异常上启动:

http://code.activestate.com/recipes/65287-automatically-start-the-debugger-on-an-exception/

它的要点是在 sys.excepthook 中安装一个异常 Hook ,它在调用时调用 pdb.pm()(尽管它比这稍微复杂一点)。

关于python - 如何将 pdb 注入(inject)失败的 Python 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4888676/

相关文章:

python - Enthought Python 发行版包含 pyhdf 和 HDF 4

python - Keras准确率和实际准确率正好相反

python - 如何为现有的 pandas 数据框添加新的索引行?

python - 获取多次上传的文件的 Django 生成的唯一名称

c# - 在服务器上运行时如何调试 .cs 代码文件?

ios - Xcode 6 忽略断点 - Swift

python - 扩展python日志处理程序,得到 "Level not an integer or a valid string"

python - 用户在 Django 的哪个位置成为 AnonymousUser?

python - 迪维希语 django-pisa pdf 的问题

c++ - VC++ 调试器 - 评估虚函数,CXX0052 : Error: member function not present