如何将 pdb 注入失败的 Python 脚本?
我正在开发一个 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.
问题是堆栈跟踪没有给我任何关于夹具上的哪一行导致此错误的线索。我该如何调试这个?我唯一能想到的是 ipythgon 中有一个功能,每当你执行某些操作并引发异常时,ipython 都会自动注入一个 pdb 提示符,这样你就可以一步步弄清楚发生了什么。我怎样才能用这个做到这一点? python 是否有一个命令行开关可以做到这一点?我可以在这里做什么来调试这个?
I'm working on a django project that has a large fixture which does not load:
$ 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.
The problem is that the stacktrace gives me no clue as to what line on the fixture is causing this error. How can I debug this? The only thing I can think of is there is a feature in ipythgon where whenever you execute something and it raises an exception, ipython automatically injects a pdb prompt so you can step around to figure out what happened. How can I do that with this? Is there a command line switch for python that does that? What could I do here to debug this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此 Python 食谱将安装一个在未捕获的异常时启动的调试器:
http://code.activestate.com/recipes/65287-automatically-start-the-debugger-on-an-exception/
其要点是在 sys.excepthook 中安装一个异常钩子调用时调用 pdb.pm() (尽管比这稍微复杂一些)。
This Python Recipe will install a debugger that starts on uncaught exceptions:
http://code.activestate.com/recipes/65287-automatically-start-the-debugger-on-an-exception/
The gist of it is to install an exception hook in sys.excepthook that calls pdb.pm() when called (though it's slightly more complicated than that).