简体   繁体   English

如何在Windows上使用Python + Django + PyCharm更快地调试?

[英]How do I debug faster with Python + Django + PyCharm on Windows?

Debugging Django with PyCharm. 使用PyCharm调试Django。

When I run anything in the debugger, it runs slow. 当我在调试器中运行任何东西时,它运行缓慢。

Django start up time is pretty long. Django的启动时间很长。

Don't get me wrong - I love PyCharm, as it has all the bells and whistles needed for a comfortable debugging session... and Python is still way easier and probably faster to debug, than other languages (like C). 不要误会我的意思 - 我喜欢PyCharm,因为它拥有舒适的调试会话所需的所有花里胡哨......而且Python比其他语言(如C)更容易,调试速度更快。 But even after I tuned my PostgreSQL database for the testing ( Optimise PostgreSQL for fast testing ), even if I have SSD drive and i7 quad-core CPU, even if I specifically told my antivirus software NOT to touch anything in C:\\Python27 directory and my project dir, it is still very slow. 但即使我调整了我的PostgreSQL数据库进行测试( 优化PostgreSQL进行快速测试 ),即使我有SSD驱动器和i7四核CPU,即使我特意告诉我的防病毒软件不要触及C:\\ Python27目录中的任何内容和我的项目目录,它仍然很慢。

Any ideas, how can I speed up debugging? 任何想法,我怎样才能加快调试速度?

I would love to see improvements mainly in process start-up time, because my most often use-case is when I debug a single unit test. 我很乐意看到主要在流程启动时间方面的改进,因为我最常使用的情况是调试单个单元测试时。

Run python normally but use pdb on your code. 正常运行python但在代码上使用pdb。 Something like this: 像这样的东西:

... code before ...
import pdb; pdb.set_trace()
... code after ...

It will stop the code on that point. 它将在该点停止代码。 You will need to press c (continue), q (quit) or n (next) in order to keep going. 你需要按c(继续),q(退出)或n(下一个)才能继续。 You can test expressions and check where are you by pressing l. 你可以按l来测试表达式并检查你在哪里。

The code will go probably faster, but debugging can be more painful. 代码可能会更快,但调试可能会更加痛苦。

I had the same problem a while ago until I figured out, Django and PyCharm allow you to specify to run single tests and not the complete test suite everytime I press the debug-button. 我不久前遇到了同样的问题,直到我发现,Django和PyCharm允许你指定运行单个测试,而不是每次按下调试按钮时都运行完整的测试套件。

In order to do this, simply edit your Debug configuration in PyCharm. 为此,只需在PyCharm中编辑Debug配置即可。 Change your target to point a module, a class or even a method somewhere deep down in your test files. target更改为在测试文件的内部某处指向模块,类或甚至方法。

In order to do this, ensure your directories are modules (eg a directory which has a __init__.py file in it). 为此,请确保您的目录是模块(例如,其中包含__init__.py文件的目录)。 You're now able to point specific targets in the following format: 您现在可以按以下格式指定特定目标:

django_app.tests_module.test_case.test_method

It is clear that the final target "path" depends on your project's organzation. 很明显,最终目标“路径”取决于您项目的组织结构。

Don't forget to change the target back once you're done implementing in order to run all the tests before pushing your code ;) 一旦完成实现,不要忘记更改目标,以便在推送代码之前运行所有测试;)

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

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