The easiest way to import your functions or parameters in python

Will Barillon
3 min readNov 1, 2020

Once you reach a good level in python, you’re writing your own functions or classes in order to get things done.

For instance, my work as a data scientist would be less comfortable without those two functions :

def myTrivialFunction_1():
print("I'm the most important function - ever")
def myTrivialFunction_2():
print("I'm also very important")

myTrivialFunctions are good-looking and short. One can easily copy and paste it on a notebook cell and run it so they are available. So it doesn’t hurt the eyes in your notebook or your script.

But sometimes, you write things that… well… it works, but it’s not good-looking and way too long. No one wants to have a cell like this.

If only we could just do something like :

import trivial_functions as tf

Guess what ? Introduction is over. Let’s get started !

In order to import the trivial functions in a notebook — it works on a script as well — we have two things to do :

  • create a file.py and copy-paste what we need
  • define a path so that our notebook know where the file.py is

Step 1 : creation of the python file

To create a python file, you can use either the “new file” functionnality of your favorite IDE, or create it with the terminal typing the following command :

echo >> trivial_functions.py

When creating a file with the terminal, it will create it at the path indicated at the left of the command, like so :

Once your file is created, simply copy-paste your functions or parameters in it and save it.

Step 1.5 : a bit of organization first…

As you can see on my screenshot above, my organization is quite messy. At the moment, I wouldn’t even need to go further since my file.py is at the root with my notebook Untilted.ipnyb. So I would just need to type the following :

import trivial_functions as tf

And that’s it !

However, we are data scientists. We need to organize our files and directory correctly. This is a proposition :

1 - my modules is a directory with trivial_functions.py in it, always at the top thanks to “1” in its name.

project 1 is a directory with my notebook

Now that we have an organized jupyter notebook, we can’t just import our file. We have to define the path towards it in the notebook.

Step 2 : define the path towards file.py in notebook

To do so, simply enter the following in a cell :

import syssys.path.append('D:/Notebooks/Jupyter/1 - my modules')

That’s it. I can’t believe this information is not easier to find on the internet…

Let’s check if it works now.

It worked for functions, but it can also works with parameters. For example, I used to work on a dark themed notebook before and matplotlib was not suited for this. Once I discovered how to change css settings on matplotlib, I only had to follow what we just did and I could import my dark themed matplotlib with only 1 line on my notebook.

Thanks for reading and stay tuned for my next articles !

--

--

Will Barillon

A python developer / data scientist that wants to provide useful informations to everyone.