简体   繁体   中英

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

Debugging Django with PyCharm.

When I run anything in the debugger, it runs slow.

Django start up time is pretty long.

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). 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.

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. 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. You can test expressions and check where are you by pressing 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.

In order to do this, simply edit your Debug configuration in PyCharm. Change your target to point a module, a class or even a method somewhere deep down in your test files.

In order to do this, ensure your directories are modules (eg a directory which has a __init__.py file in it). 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 ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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