Adding shell_plus
for Django to PyCharm’s Python Console
I’m blogging this because I find myself looking it up repeatedly. Every time I start a project, I eventually hit a point where I use the REPL quite a bit. Usually I want to use it from PyCharm’s python console. And I usually find myself manually typing 5 or 6 commands at the start of each session that shell_plus
from django-extensions
would give me for free.
This answer on this stackoverflow question is still correct as of PyCharm 2022.1.3. I’m just hoping to make it easier to find each time I want it by putting it here.
Go to Settings (Preferences on Mac) -> Build, Execution, Deployment -> Django Console and append the following to the “Starting Script”:
# https://stackoverflow.com/questions/19868449/run-shell-plus-through-pycharm
from django_extensions.management import shells
from django.core.management.color import color_style
imported_items = shells.import_objects({}, color_style())
for k, v in imported_items.items():
globals()[k] = v
The new settings will take effect once you quit and relaunch PyCharm. If you’ve never used shell_plus, it imports all of your models and a slew of django utilities, including reverse()
. It gives me 90% of what I need in the console without needing to type an import statement. If hot reload worked there, it’d be perfect.
I’m trying on Kev Quirk’s “100 Days To Offload” idea. You can see details and join yourself by visiting 100daystooffload.com.
This is day 5.