Bradley Kirton's Blog

Published on Nov. 22, 2022

Go home

Auto-loading modules in manage.py shell

Suppose you have a django project with an application named yourapp with the following structure:

yourapp/
├── __init__.py
├── admin.py
├── apps.py
├── models
├── urls.py
└── views.py

To auto-load your models when running the python manage.py shell command create a .pythonrc.py file with the following content.

#.pythonrc.py

import logging

logging.basicConfig(level=logging.INFO)

from yourapp.models import *  # noqa

Now when you run PYTHONSTARTUP=.pythonrc.py python manage.py shell your project models will be automatically available.