How to reload modules in django shell?

后端 未结 11 1187
面向向阳花
面向向阳花 2020-12-22 18:38

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 ma

11条回答
  •  Happy的楠姐
    2020-12-22 18:53

    Use shell_plus with an ipython config. This will enable autoreload before shell_plus automatically imports anything.

    pip install django-extensions
    pip install ipython
    ipython profile create
    

    Edit your ipython profile (~/.ipython/profile_default/ipython_config.py):

    c.InteractiveShellApp.exec_lines = ['%autoreload 2']
    c.InteractiveShellApp.extensions = ['autoreload']
    

    Open a shell - note that you do not need to include --ipython:

    python manage.py shell_plus
    

    Now anything defined in SHELL_PLUS_PRE_IMPORTS or SHELL_PLUS_POST_IMPORTS (docs) will autoreload!

    Note that if your shell is at a debugger (ex pdb.set_trace()) when you save a file it can interfere with the reload.

提交回复
热议问题