I'm currently developping a Django application and I like using the pdb to know in which state is my application and some stuff like that. I would like to have all the amazing capabilites of BPython inside the debuger... Like autocompletion and things like that.
Is that even possible ? Thanks :)
Asked
Active
Viewed 420 times
1

Nick Sweeting
- 5,364
- 5
- 27
- 37

Depado
- 4,811
- 3
- 41
- 63
-
1I use ipython and if its installed in the environment django shell automatically picks it up . I am sure that would be the case for bpython – dusual Aug 22 '13 at 16:12
-
@dusual Based on your comment, I tried bpython. You were right. Works out the box. – Jonathan Spiller Oct 03 '20 at 13:05
2 Answers
2
I know this is quite old but I still couldn't find a solution I liked, and ended up with the solution below which uses a django command.
The other answer by Nick works as well too, but I don't like having to add Django specific things in my global .pythonrc
#myapp/management/commands/bshell.py
from django.core.management.base import BaseCommand
from django.apps import apps
class Command(BaseCommand):
help = "Runs the bpython interactive interpreter if it's installed."
requires_model_validation = False
def handle(self, *args, **options):
loaded_models = apps.get_models()
models = {}
for model in loaded_models:
models[model.__name__] = model
import bpython
bpython.embed(models)
.venv ❯ python manage.py bshell
>>> Locat
┌───────────────────────────────────────────────────────────────────────────────────┐
│ Location( │
└───────────────────────────────────────────────────────────────────────────────────┘

gtalarico
- 4,409
- 1
- 20
- 42
1
Put some code in your python repl startup file to detect you're in a Django project, and perform the necessary imports:
put this in your ~/.bashrc or ~/.bash_profile
export PYTHONSTARTUP=~/.pythonrc
Create or edit your
~/.pythonrc
:try: from django.core.management import setup_environ import settings setup_environ(settings) print 'imported django settings' except: pass
OR Use this more involved snippet that imports all your django modules and works in project subdirectories here: https://gist.github.com/pirate/2659b242bded82c3c58f2458e6885738#file-pythonrc-L56

Nick Sweeting
- 5,364
- 5
- 27
- 37