How to reload modules in django shell?

后端 未结 11 1109
面向向阳花
面向向阳花 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条回答
  • 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.

    0 讨论(0)
  • 2020-12-22 18:53

    Using a combination of 2 answers for this I came up with a simple one line approach.

    You can run the django shell with -c which will run the commands you pass however it quits immediately after the code is run.

    The trick is to setup what you need, run code.interact(local=locals()) and then re-start the shell from within the code you pass. Like this:

    python manage.py shell -c 'import uuid;test="mytestvar";import code;code.interact(local=locals())'
    

    For me I just wanted the rich library's inspect method. Only a few lines:

    python manage.py shell -c 'import code;from rich import pretty;pretty.install();from rich import inspect;code.interact(local=locals())'
    

    Finally the cherry on top is an alias

    alias djshell='python manage.py shell -c "import code;from rich import pretty;pretty.install();from rich import inspect;code.interact(local=locals())"'
    

    Now if I startup my shell and say, want to inspect the form class I get this beautiful output:

    0 讨论(0)
  • 2020-12-22 18:58

    My solution to it is I write the code and save to a file and then use:

    python manage.py shell < test.py

    So I can make the change, save and run that command again till I fix whatever I'm trying to fix.

    0 讨论(0)
  • 2020-12-22 19:01

    Reload() doesn't work in Django shell without some tricks. You can check this thread na and my answer specifically:

    How do you reload a Django model module using the interactive interpreter via "manage.py shell"?

    0 讨论(0)
  • 2020-12-22 19:07

    Instead of running commands from the Django shell, you can set up a management command like so and rerun that each time.

    0 讨论(0)
  • 2020-12-22 19:11

    look at the manage.py shell_plus command provided by the django-extensions project. It will load all your model files on shell startup. and autoreload your any modify but do not need exit, you can direct call there

    0 讨论(0)
提交回复
热议问题