How do I preload imports into Django's manage.py shell command?
Asked Answered
S

5

13

When I run manage.py shell on my Django project to take a peek at something there are common imports that I always want to run at the start of the shell (e.g. I always want to import my model files.) How can I have these run automatically everytime I run the shell command?

2nd related question, when I hit the up arrow I get the "^A" character instead of the previously run command in the manage.py shell (and in my regular python shell), how can I fix this so it loads the previous command like on the Linux/Unix command line?

Steffi answered 2/12, 2009 at 22:37 Comment(0)
L
11

For the first question, look at the manage.py shell_plus command provided by the django-extensions project. It will load all your model files on shell startup. The project has got a whole load of other useful tricks too, so it's definitely worth checking out.

For the second question, I can only guess that you need to install readline.

Luminance answered 2/12, 2009 at 23:3 Comment(0)
M
4

you can replicate what

python manage.py shell

does by just doing:

from django.core.management import setup_environ
from mysite import settings
setup_environ(settings)

and you will have the environment all set up for the rest of that script. There are some other ways to do this here too: http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/

Misspell answered 16/6, 2011 at 6:41 Comment(1)
In my case, 'ImportError: cannot import name 'setup_environ' shows to meEthiopian
H
2

One single thing which can solve both of your problem is ipython. ipython stores the previous executions and it can give you completion as well.

Hiphuggers answered 3/12, 2009 at 16:3 Comment(1)
Saving history doesn't solve the first problem when you are constantly running on a new instance (e.g., heroku), whereas shell_plus does.Amp
M
2

Auto importing frequently used packages in python manage.py shell_plus

Example:

#  local_settings
SHELL_PLUS_PRE_IMPORTS = (
    ('<app_name>.models', '*'),
    ('<app_name>.forms', '*'),
    ('<app_name>.views', '*'),
    ('django.core.urlresolvers', '*'),
    ('django.db', 'connection'),
    ('django.db', 'reset_queries'),
)

SHELL_PLUS_DONT_LOAD = ['<app_name>', '<app_name>']

Reference:
https://github.com/django-extensions/django-extensions/blob/master/docs/shell_plus.rst

Mcniel answered 24/8, 2015 at 7:30 Comment(0)
S
0

Adding extra stuff to the django shell can be done using as a starting point the shell_plus command provided in the django-extesions app, and modifying it by adding whatever you want to make available to the 'imported_objects' dictionary.

Eg if you duplicate the shell_plus.py file and add these two lines at the end:

# .......
alist = range(1000)
imported_objects['alist'] = alist
code.interact(local=imported_objects)  # <-- this is the original final line

When you run the shell using the new file the 'alist' reference will be available. I put a longer example here: http://www.michelepasin.org/techblog/?p=1012

Superstar answered 17/11, 2010 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.