Debugging Django Management Commands in PyCharm
Tags: django, debugging, pycharm
Published: 2018-02-10 22:38:13

---
title: "Debugging Django Management Commands in PyCharm"
published: true
tags: Django,Debugging,PyCharm
canonical_url: http://djangotricks.blogspot.com/2016/05/debugging-management-commands-in-pycharm.html
---

 ![](https://3.bp.blogspot.com/-k9c5SDqv8-k/WFCk9yVTdGI/AAAAAAAABwE/xo8B7TUEQI4OG48aU3-v7YAiNIVZBy62wCPcB/s1600/Debugging%2BDjango%2BManagement%2BCommands%2Bin%2BPyCharm.png) 

My favorite editor for Python projects is PyCharm. Besides [editing code](http://djangotricks.blogspot.de/2013/09/7-powerful-features-of-pycharm-editor-i.html), it allows you to inspect the database, work with Git repositories, run management commands, execute bash commands and Python scripts, and debug code just in the same window. In this article, I will show you how to set breakpoints and debug Django management commands visually in PyCharm.

 

Django management commands are scripts that can be executed on your Django project to do something with the project database, media files, or code. Django itself comes with a bunch of commands like: `migrate`, `runserver`, `collectstatic`, `makemessages`, and `clearsessions`. Management commands can be executed like this:

 
```
(myproject_env)$ python manage.py clearsessions
```
 

If you want to create a custom management command in your project, you can find how to do that in the [official Django documentation](https://docs.djangoproject.com/en/1.9/howto/custom-management-commands/). Also you can find some practical examples in the _Chapter 9, Data Import and Export_ of the _[Web Development with Django Cookbook - Second Edition](https://www.packtpub.com/web-development/web-development-django-cookbook-second-edition)_.

 

In this example, I won't create any new management command, but will debug the `clearsessions` command that is coming from Django and is located at `django/contrib/sessions/management/commands/clearsessions.py`.

 

First of all, let's click on "Edit Configurations..." in the top toolbar just before the Run button (with the Play icon). In the opened dialog box "Run/Debug Configurations" click on the "Add New Configuration" button (with the Plus icon) and choose "Python".

 

Let's fill in the configuration with these values:

```
Name: Clear Outdated Sessions
Script: /Users/me/DjangoProjects/myproject\_env/project/myproject/manage.py
Script paramethers: clearsessions --traceback --verbosity=2
Python interpreter: Python 2.7.6 virtualenv at ~/DjangoProjects/myproject\_env
Working directory: /Users/me/DjangoProjects/myproject\_env/project/myproject/
```
 
[![](https://3.bp.blogspot.com/-u8baIcTnHwk/Vz4goxWjkNI/AAAAAAAABsk/MSuysY8Kg6AlOe3qcvsp47J2E6FV90dGQCLcB/s1600/1.setting_executable_python_script.png)](https://3.bp.blogspot.com/-u8baIcTnHwk/Vz4goxWjkNI/AAAAAAAABsk/MSuysY8Kg6AlOe3qcvsp47J2E6FV90dGQCLcB/s1600/1.setting_executable_python_script.png)
 

Then open the file with the definition of the management command `django/contrib/sessions/management/commands/clearsessions.py`. Click on the left padding of the editor to add a breakpoint (marked with a red circle) where the script should stop executing for inspection.

 

Normally to run this script, you could click on the Run button (with the Play icon). But as we want to debug the script, we will click on the Debug button (with the Bug icon) in the toolbar.

 
[![](https://2.bp.blogspot.com/-RV3oaSW6ZyM/Vz4gowVfXaI/AAAAAAAABsg/MS8uCiywY-YfWGl83rbRtDJCulVs_7_qACLcB/s1600/2.debugging.png)](https://2.bp.blogspot.com/-RV3oaSW6ZyM/Vz4gowVfXaI/AAAAAAAABsg/MS8uCiywY-YfWGl83rbRtDJCulVs_7_qACLcB/s1600/2.debugging.png)
 

The script will start executing and will stop temporarily at the breakpoint you made. You will be able to inspect all local variables in the debug panel that is opened at the bottom of your window by default.

 

You can navigate through code execution with the arrow buttons "Step Over", "Step Into", etc. To evaluate local or global variables or values, click on the "Evaluate Expression" button (with the Calculator icon) and enter some Python code.

 
[![](https://2.bp.blogspot.com/-L4101od4F-0/Vz4gokzOUPI/AAAAAAAABsc/HeM6VDgISg4uI30dilwxudWY6gDL5JgxwCLcB/s1600/3.inspecting_variables.png)](https://2.bp.blogspot.com/-L4101od4F-0/Vz4gokzOUPI/AAAAAAAABsc/HeM6VDgISg4uI30dilwxudWY6gDL5JgxwCLcB/s1600/3.inspecting_variables.png)
 

Click on the "Resume Program" button (with the Fast Play icon) to continue execution of the script, when you are ready.

 

Analogously, you can debug your models, views, forms, or middlewares by running a development server ("Django server") in debug mode.


_This post was originally published on [djangotricks.blogspot.com](http://djangotricks.blogspot.com/2016/05/debugging-management-commands-in-pycharm.html)_