When trying to decide where to eat, one of the go-to places my friends and I choose is FIVE, a local restaurant and bar. With its reasonable prices, relaxed atmosphere, and great food selection, FIVE…


Importing local Python modules into Jupyter Notebooks

I find working in a Jupyter Notebook very useful when developing any type of python code. The instant feedback of new written code help me a lot to iterate quickly and debug certain snippets.

There is a small tweak that you can do to also import your python modules and functions while developing in notebooks. It is very simple but I always struggle to remember it and found it in google

Basically, we have two parts.

The first one is the autoreload module. This is an extension that is installed by default with Jupyter. It allow us (as its name suggest) to autoreload the imported modules when there is any changes. Otherwise, you will need to restart the kernel.

The second part is to add the path to your source code to your PYTHONPATH so it can be imported. The easiest way of do this is to insert the math in with the sys.path.insert command. the 0 is the index to insert the path. We put it in the first place so it takes preference over anything else you might have installed.

As a bouns track, I also like to add my environment variables in the notebooks itself when I am debuging something. It as easy as:

Of course, be careful and don’t put any secret in the code.

This is a ritual setup that I always do when creating a new notebook. Maybe the next step is to automatize it…

Add a comment

Related posts:

When the World Looked Bleak I Danced

It was the Autumn of 2020 and during the peak of the pandemic. I remember feeling solace in my solitude. Serenity in my sonic sanctuary. The Covid-19 cases were rising again and we all were just… Read more

A Duck Hears Also

My inner critic distracts me with loud stories about my flaws. My inner critique helps me with objective feedback about how I could solve a problem. Read more

Walk Like the Dead in Walking Dead Tees

The fall TV season is going strong now, and Halloween’s right around the corner already. And the 8th season of The Walking Dead is right around the corner, too, debuting with its long-awaited 100th… Read more