Further information#

Why modularise?#

Best practice when writing code is to break up code in to modular parts. One guiding principle described in [Fowler, 2018]:

“Code should be obvious. When someone needs to make a change, they should be able to find the code to be changed easily and make the change quickly without introducing any errors.”

Whilst this guiding principle is ambiguous and all concepts related to clean code writing and refactoring are not things that can be covered in this book one specific principle is the one referred to in [Martin, 2009]:

“Functions should do one thing. They should do it well. They should do it only.”

Note

In some texts on code architecture you will see arbitrary rules about how many lines of code should be in a given function. Having a function with 10 or more lines of could might indicate that it can be modularised. However, I do not recommend following such rules as sometimes they might add more complexity than they remove. I recommend you stick to the principles of making your code clear and ensuring your functions do one thing well and one thing only.

Why do I get an import error?#

The most probable explanation for this is that you are importing a file that is not in the same directory or that you have not imported the file with the correct name.

If we assume that your code is in a library.py directory but that your notebook is in a different directory then you will get an error as shown below:

import library
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 import library

ModuleNotFoundError: No module named 'library'

Similarly if you perhaps incorrectly saved your library.py file with a typo in the name such as: librery.py then you would get the same error.

How do I make my file importable from other directories?#

This falls under the subject matter of “packaging”. We will not cover this in this book but some good information on the practice is available at the following: