Skip to content

#8: Modules

I got a lot of topics covered over the last weeks. To put that to use, we need to look at ways to organise our code. Therefore, I use this post to explain modules and how they can help us to make our code maintainable.

Modules

A module in Python is basically a file with Python code inside. The module should have a short, all-lowercase name and use underscores if it makes it more readable. Since there is no language construct to name a module, the filename will be the name of the module (except the extension that you should set to .py).

A module can contain executable statements and function definitions. The statements will only be executed once (the first time you import the module).

1
2
3
4
5
6
def say_hello(name):
    print(f"Hello {name}")


def say_goodbye(name):
    print(f"Goodbye {name}")

Importing a module

We have a lot of flexibility for importin a module. If we want everything directly accessible, we can write this:

1
2
3
4
from hello import *

say_hello("Johnny")
say_goodbye("Johnny")

However, this may lead to a collision with other functions that have the same name. A better way is to import everything but forces you to use the module name as a prefix:

1
2
3
4
import hello

hello.say_hello("Johnny")
hello.say_goodbye("Johnny")

It is possible just to import the functions we need:

1
2
3
from hello import say_hello

say_hello("Johnny")

To import multiple functions, we can specify a tuple with all the things we want:

1
2
3
4
from hello import (say_hello, say_goodbye)

say_hello("Johnny")
say_goodbye("Johnny")

We can even give the imported function a new and shorter name:

1
2
3
from hello import say_hello as hi

hi("Johnny")

With those different ways we always should be able to import exactly what we need.

Executing a module as a script

We not only can import our modules, we can run them as scripts. All we need to do is to type python followed by the module name (given we are in the same folder):

$ python hello.py

We most likely do not want to have the same behaviour with our module when we import it and when we run it as a script. In the case of the script, we most likely want it to do something useful right there, while when we import it, we want to be able to work with the functions.

There is the special variable called __name__, that helps us to figure out in which mode our module is executed. To test it we add this line to the end of our module hello.py:

print(__name__)

When we run it from the command line, we get this output:

$ python hello.py
__main__

While we see this output when we import our module:

>>> import hello
hello

With this different output (the module name when we import, __main__ when we directly execute it) we can set a useful example in the script version. To do that we replace the print(__name__) line with this:

if __name__ == "__main__":
    say_hello("Marvin") 

We now can import our module without any output and when we run it directly, it prints this:

$ python hello.py
Hello Marvin

The module search path

Python will search these places to find a module you try to import:

  1. The current directory
  2. The folders in the PYTHONPATH variable
  3. The installation-dependent default

You can see which directories all are searched by printing the sys.path variable:

>>> import sys
>>> print(sys.path)

Next

Python is an object-oriented programming language and so far I did nothing with classes. It is time to change that.