46๐
I recommend using the django-extensions project like stated above by dongweiming. But instead of just โshell_plusโ management command, use:
manage.py 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.
125๐
Iโd suggest use IPython autoreload extension.
./manage.py shell
In [1]: %load_ext autoreload
In [2]: %autoreload 2
And from now all imported modules would be refreshed before evaluate.
In [3]: from x import print_something
In [4]: print_something()
Out[4]: 'Something'
# Do changes in print_something method in x.py file.
In [5]: print_something()
Out[5]: 'Something else'
Works also if something was imported before %load_ext autoreload
command.
./manage.py shell
In [1]: from x import print_something
In [2]: print_something()
Out[2]: 'Something'
# Do changes in print_something method in x.py file.
In [3]: %load_ext autoreload
In [4]: %autoreload 2
In [5]: print_something()
Out[5]: 'Something else'
There is possible also prevent some imports from refreshing with %aimport
command and 3 autoreload strategies:
%autoreload
- Reload all modules (except those excluded by %aimport) automatically
now.%autoreload 0
- Disable automatic reloading.
%autoreload 1
- Reload all modules imported with %aimport every time before executing
the Python code typed.%autoreload 2
- Reload all modules (except those excluded by %aimport) every time
before executing the Python code typed.%aimport
- List modules which are to be automatically imported or not to be
imported.%aimport foo
- Import module โfooโ and mark it to be autoreloaded for %autoreload 1
%aimport -foo
- Mark module โfooโ to not be autoreloaded.
This generally works good for my use, but there are some cavetas:
- Replacing code objects does not always succeed: changing a @property in a class to an ordinary method or a method to a member variable can cause problems (but in old objects only).
- Functions that are removed (eg. via monkey-patching) from a module before it is reloaded are not upgraded.
- C extension modules cannot be reloaded, and so cannot be autoreloaded.
- [Django]-Using the reserved word "class" as field name in Django and Django REST Framework
- [Django]-How to set a Django model field's default value to a function call / callable (e.g., a date relative to the time of model object creation)
- [Django]-Why does my Django admin site not have styles / CSS loading?
55๐
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.
- [Django]-In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?
- [Django]-Http POST drops port in URL
- [Django]-Django โ how to visualize signals and save overrides?
33๐
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
- [Django]-Pagination in Django-Rest-Framework using API-View
- [Django]-Redirect to Next after login in Django
- [Django]-How can I build multiple submit buttons django form?
26๐
It seems that the general consensus on this topic, is that python reload() sucks and there is no good way to do this.
- [Django]-Delete multiple objects in django
- [Django]-Timestamp fields in django
- [Django]-Set up a scheduled job?
15๐
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.
- [Django]-CSV new-line character seen in unquoted field error
- [Django]-Django removing object from ManyToMany relationship
- [Django]-Django project models.py versus app models.py
8๐
My solution for this inconvenient follows. I am using IPython.
$ ./manage.py shell
> import myapp.models as mdls # 'mdls' or whatever you want, but short...
> mdls.SomeModel.objects.get(pk=100)
> # At this point save some changes in the model
> reload(mdls)
> mdls.SomeModel.objects.get(pk=100)
For Python 3.x, โreloadโ must be imported using:
from importlib import reload
Hope it helps. Of course it is for debug purposes.
Cheers.
- [Django]-Django: guidelines for speeding up template rendering performance
- [Django]-Dynamic choices field in Django Models
- [Django]-What's the best way to extend the User model in Django?
4๐
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"?
- [Django]-How to set environment variables in PyCharm?
- [Django]-Django Template Language: Using a for loop with else
- [Django]-Allowing only super user login
3๐
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:
- [Django]-Django dynamic model fields
- [Django]-Django {% if forloop.first %} question
- [Django]-Django โ how to unit test a post request using request.FILES
1๐
Instead of running commands from the Django shell, you can set up a management command like so and rerun that each time.
- [Django]-How do I get the object if it exists, or None if it does not exist in Django?
- [Django]-How do I deploy Django on AWS?
- [Django]-Django ManyToMany filter()
1๐
Not exactly what you want, but I now tend to build myself management commands for testing and fiddling with things.
In the command you can set up a bunch of locals the way you want and afterwards drop into an interactive shell.
import code
class Command(BaseCommand):
def handle(self, *args, **kwargs):
foo = 'bar'
code.interact(local=locals())
No reload, but an easy and less annoying way to interactively test django functionality.
- [Django]-Django: How to get related objects of a queryset?
- [Django]-Django stops working with RuntimeError: populate() isn't reentrant
- [Django]-Django 2 โ How to register a user using email confirmation and CBVs?
0๐
import test // test only has x defined
test.x // prints 3, now add y = 4 in test.py
test.y // error, test does not have attribute y
solution Use reload from importlib as follows
from importlib import reload
import test // test only has x defined
test.x // prints 3, now add y = 4 in test.py
test.y // error
reload(test)
test.y // prints 4
- [Django]-How to manually assign imagefield in Django
- [Django]-Define css class in django Forms
- [Django]-Class has no objects member