Automatically import models on Django shell launch

Question:

I’m tired of typing from account_import.models import ImportFile every time I open my Django shell. Is there a way to automatically run this command whenever I fire up my shell?

Asked By: Jason Swett

||

Answers:

http://docs.python.org/using/cmdline.html#envvar-PYTHONSTARTUP

If you set the environment variable PYTHONSTARTUP to a file, this will be run first whenever you start a python shell.

Answered By: Daren Thomas

install django-extensions, one of the commands it features (shell_plus) is providing the context for your models.
https://github.com/django-extensions/django-extensions

So, instead of ./manage.py shell you can use ./manage.py shell_plus so that everything is imported.

Answered By: Uku Loskit
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.