Using Django in Jupyter Notebook

Emmanuel
Emmanuel
Oct 18, 2018 · 1 min read

In my last story, I related how to install a kernel in a virtual environment in order to run Jupyter from there.

https://medium.com/@eleroy/jupyter-notebook-in-a-virtual-environment-virtualenv-8f3c3448247

This was a necessary step to run Django in Jupyter Notebook

So now to run Django, we need to set a few more things

In the Notebook, we need to initialize Django this way:

import os# this environment variable points to the 'settings' module 
# in the Django app.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings")
# maybe not be necessary if you hard coded you DB URL in settings,
# but mine it pulled from the DATABASE_URL environment variable
os.environ.setdefault("DATABASE_URL",
"postgres://localhost:5432/db")
# and then...
import django
django.setup()

That’s it!

Emmanuel

Written by

Product Manager, Solution Architect, Entrepreneur, passionate about making sense of data.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade