It’s as Easy as Pie!

Ben McCarty
4 min readJun 8, 2021

I take a lot of pride in being efficient. If I can make something easier to implement while retaining functionality, I’ll do it! This is particularly true when it comes to starting a new project and my dreaded question, “Where do I start?” Of course, the first thing we do for any notebook is to import our packages. Numpy, Pandas, Seaborn, Matplotlib…. We get them loaded into our notebook and ready to feast on our data to create nice, easy-to-understand DataFrames and visuals.

You could say it’s as easy as pie!

Now this is where things get fun, dear reader, as we are embarking on a voyage of discovery together! Yes, your fearless author will be joining you as we attempt to bake a pie.

But baking?! This is CODING. And besides, do pythons really like pie? What about pandas??

It doesn’t really matter, because YOU will come to like it, or at least the .py files we are going to make together. We are going to create our own .py file to store a sample function, then set up our own mini package to be able to import that function into a notebook.

Baking a .Py

First, gather the ingredients:

  • A new folder containing an empty Jupyter notebook
  • A new folder inside the same folder as the notebook
  • A blank .txt file saved inside the blank folder
  • Your imagination (and patience with my baking analogy)

Next, start by preheating your oven….

No wait, don’t do that. But let’s go ahead and open that .txt file and mix in a bit of code. If we type in the following and save it…

Yummy pie!

…We will have a .txt file with a function. Progress! Let’s rename the file to “recipes.py” and save it. If the code works, we’ll have a printed string confirming that we made our .py file correctly. Simple and sweet.

Now we have our .py filling (‘recipes.py’) ready to be baked into a file and served in our notebook. The next step is to bring the pie to the table!

In order to bring our .py file over to a notebook, we need to use the folder within the same folder containing our notebook. This will be our module, and let’s call it “recipe_book” for fun (since it’s for our functions, get it?).

Easy-Code Recipes

Make sure the recipe.py file is in the “recipe_book” folder and create a new file called “__init__.py” (make sure there are TWO underscores, just like when you define a class). Like the classy baker we are, we want to make sure everyone knows who made the pie and where the recipe came from. At the top, type in ‘’’A collection of functions created by [YOUR NAME] as part of a baking analogy.’’’ Make sure to use three apostrophes! Sweeten to taste.

On a new line in our __init__.py file, type “from recipe_book import recipes” (without quotes) and save the file.

We now have our own recipe book and created our own recipe! Grandma would be so proud. *sniffs and wipes away tear*

Bringing it to the table

Right now, we have our function/recipe (bake_a_pie), in a .py script (recipes.py), saved into a module (recipe_book) with our classy __init__.py file telling Python where to find our recipe when we’re ready to call our function (a.k.a. bake the pie). Let’s dust off the flour and get our just desserts!

Open up the blank notebook stored inside the same folder as our module. In the first cell, type “from recipe_book import recipes” and shift-enter. Tada! We now have our recipe book in hand and we’re ready to bake!

In the next cell, type: “recipes.bake_a_pie()” and shift-enter to bake the pie. If everything works, Grandma won’t come after us with a wooden spoon for bringing dishonor to the family name. And by that, of course, I mean we will have our “Freshly-baked .Py!” printout. If it doesn’t, we will be left with the burnt taste of disappointment (and some bruises from Grandma’s spoon).

But it should work!

YAY!

Now go enjoy the sweet taste of growing your coding skills! Share your functions across other notebooks and with others!

--

--