Fix Python – How to reload modules in django shell?


Asked By – Mad Wombat

I am working with Django and use Django shell all the time. The annoying part is that while the Django server reloads on code changes, the shell does not, so every time I make a change to a method I am testing, I need to quit the shell and restart it, re-import all the modules I need, reinitialize all the variables I need etc. While iPython history saves a lot of typing on this, this is still a pain. Is there a way to make django shell auto-reload, the same way django development server does?

I know about reload(), but I import a lot of models and generally use from app.models import * syntax, so reload() is not much help.

Now we will see solution for issue: How to reload modules in django shell?


I recommend using the django-extensions project like stated above by dongweiming. But instead of just ‘shell_plus’ management command, use: shell_plus --notebook

This will open a IPython notebook on your web browser. Write your code there in a cell, your imports etc. and run it.

When you change your modules, just click the notebook menu item ‘Kernel->Restart’

There you go, your code is now using your modified modules.

This question is answered By – mpaf

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0