# How does runserver restart even after a SyntaxError?

**URL:** <https://forum.djangoproject.com/t/how-does-runserver-restart-even-after-a-syntaxerror/27627>\
**Category:** Getting Started\
**Created:** [January 31, 2024, 2:41pm UTC](https://forum.djangoproject.com/t/how-does-runserver-restart-even-after-a-syntaxerror/27627 "2024-01-31T14:41:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lplawlor](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/lplawlor/32/18875_2.png) [@lplawlor](https://forum.djangoproject.com/u/lplawlor)\
**Post date:** [January 31, 2024, 2:41pm UTC](https://forum.djangoproject.com/t/how-does-runserver-restart-even-after-a-syntaxerror/27627/1 "2024-01-31T14:41:03Z")

</div>

We’ve all run the `runserver` management command in development. Django of course prints something like:

```auto
Loading .env environment variables...
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
January 31, 2024 - 14:25:18
Django version 5.0.1, using settings 'project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

```

Of course, this command uses the [`django.utils.autoreload`](https://github.com/django/django/blob/main/django/utils/autoreload.py) module to watch for file changes and reload the development server to reflect the changes.

However, there’s one useful feature of `runserver` that isn’t explained by `autoreload` alone. If a `SyntaxError` is introduced somewhere in the code, a traceback will be printed to the console as expected, however the `runserver` command will not end. Fixing the `SyntaxError` and saving the file will still trigger a reload:

```auto
...
SyntaxError: expected ':'
/home/username/project/app/views.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
January 31, 2024 - 14:34:04
Django version 5.0.1, using settings 'project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

```

What I don’t understand is how to replicate this behaviour in my own management commands. I can copy the [`runserver` source code](https://github.com/django/django/blob/main/django/core/management/commands/runserver.py) directly into my own custom management command called `foo.py`, and this behaviour will not carry over. `foo` will watch for file changes and restart just fine, but if a `SyntaxError` is introduced, it crashes altogether and spits me back into the shell.

Is there anyone with a better knowledge of the `runserver` command who can to shine a light on this?

---

<div class="post-metadata">

**Author:** ![IgorArnaut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/igorarnaut/32/26771_2.png) [@IgorArnaut](https://forum.djangoproject.com/u/IgorArnaut)\
**Post date:** [April 10, 2025, 4:31pm UTC](https://forum.djangoproject.com/t/how-does-runserver-restart-even-after-a-syntaxerror/27627/2 "2025-04-10T16:31:01Z")

</div>

I have the same issue. I have set VS Code to auto-save on change. While running `runserver`, sometimes when I edit a .py file, runserver gets stuck at a error, even though I’ve fixed that error. I have to stop `runserver` and run it again.
