How to debug Django commands in PyCharm
Solution 1
You can debug a custom Django admin/management command in PyCharm by creating a custom Django server entry on the Run/Debug Configuration menu:
- Click
Edit Configurations...
. - Click the plus sign and choose
Django server
. - Fill in the
Name
as you please, clear theHost
andPort
fields, checkCustom run command
and enter the name of your command to the right of the checkbox. - Enter any extra command-line arguments into the separate field
Additional options
, not appended in the run command. - Click OK.
Now set a breakpoint, choose your new configuration from the Run/Debug Configuration menu and click the Debug button. Et voilà!
Solution 2
Since clearing Host and Port will not make the command run at all (PyCharm 5), the solution I found is to use a Python run configuration instead of a Django server. Fill Script
with your manage.py script, other parameters in Script Parameters
, and adjust your environment such as Working directory
.
Solution 3
I am explaining using my following custom Django command:
python manage.py execute_algorithm -f input_data.json
Steps to configure Django Command: Step: From Django toolbar go to:
Run > Edit Configurations
Click on the '+' icon at top left, to create new command > select 'Django server' from the drop down.
Fill following details:
Name: any suitable name that you want to give to this config e.g. execute_algorithm_command
Host: Clear the field
Port: It's 8000 by default, clear it.
Custom Run Command: Check this box fist. Provide your command name there. You can get that from
apps/module/management/command/execute_algorithm
. e.g value: execute_algorithm
Additional options: Whatever is there, after you command name. value is: -f input_data.json
Taras Bilynskyi
Updated on September 10, 2021Comments
-
Taras Bilynskyi almost 3 years
I know how to run commands with PyCharm (Tools -> Run manage.py Task), but I would like to debug them also, including my commands and third party app's commands.
-
Jonathan Geisler almost 10 yearsEmphasis for people like me who apparently can't read properly: the Host and Port fields have to be cleared, otherwise the custom command will not work!
-
Frikster over 8 yearsSorry, but what am I supposed to put into "custom run command?" I called mine "simple_chart" but I get "unknown command" when I try to debug. I have a method in views.py called simple_chart that I have placed a break point in. All I want is for the debugger to halt there so I can explore the variables in the local environment.
-
Fish Monitor over 8 yearsThe run window does not show if I clear the Host and Port fields.
-
Yunti about 8 yearsThis is what got it working for me, (pycharm 2016). Put manage.py in script, your django management command in script parameters (together with any django mangement command flags) and change the working directory.
-
Caleb_Allen about 8 years@DirkHaupt you've probably solved it at this point but I found that using underscores threw off commands sometimes.
-
Dejell about 8 yearsI don't have Django server option in the plus
-
Adam Silver over 7 yearsHow do I pass multiple positional options?
-
texnic over 7 yearsFor those who are struggling: the "custom run command" box should contain only your command. E.g. if you are running
py manage.py cmd
from console, this box will contain simplycmd
. -
PeterN over 7 years@Dejell : you need the professional version of Pycharm for DJANGO support. ( jetbrains.com/pycharm/features/editions_comparison_matrix.html )
-
maniexx about 6 yearsAlso make sure not to have whitespace after the command name - it will wrap it in quotes and not work.
-
miken32 almost 3 yearsHow does this differ from the accepted answer posted 7 years earlier?
-
fang_dejavu over 2 years@miken32 Looking back the first one has everything one needs, but for some reason, I did not get it. This answer accomplishes the same as the first one but uses an example. BTW some of us understand things explained differently.
-
user3250386 over 2 yearsFor anyone else having trouble finding it, the Edit Configurations option is found under the Run menu.