Modules

Hey guys, welcome back to the Python basics tutorial! Today I'll show you what modules are.

What are modules?

When your program gets larger, your file gets messier. In this case you may want to split the project into separate files. For example, when making a graphical calculator, you may want to separate the user interface and logic. So, you split them into files called modules. A module is just a file with Python code which can be run from another file by importing. How do we import a module?

import module

Simple. Let's try to make a timer with the time module that comes by default with Python. First we import time.

import time

One important thing to remember, a module is a lot like a normal object. You can access functions defined in the module just like methods of an object. Now, to make a timer, we need to delay the execution of the code for the duration the user specified.

To delay the execution we use time's sleep function, which basically stops execution and "sleeps" for some time. sleep takes 1 argument: the duration in seconds.

So, to delay for 1 second, we do time.sleep(1). Really simple. Let's add it to our code.

duration = int(input('How many seconds? > ')) # Suppose the user inputs 3
print('Starting...')
time.sleep(duration)
print('Slept for ' + duration + ' seconds.')

Full code until now:

import time
duration = int(input('How many seconds? > '))
print('Starting...')
time.sleep(duration)
print('Slept for ' + duration + ' seconds.')

Easy. There are a lot more modules that are installed with Python. These modules are collectively called "The Standard Library".

OK, now you know how to import modules, and how to access functions inside them. There's another useful module: string which is also available in the Standard Library. As usual, you import it by :

import string

Now, string has some useful variables already defined. When you need a string of all the letters in English, instead of letters = 'abcdefgh...' you can just use string's attribute ascii_lowercase.

import string
print(string.ascii_lowercase)
# Prints 'abcdefghijklmnopqrstuvwxyz'

OK. We've learned about some pretty useful modules there.

There are a lot more modules available in the Standard Library for you to use. Here's a list: https://docs.python.org/3.6/py-modindex.html

Creating your own modules

Alright, now it's time to make your own modules. Create a new directory/folder. Place two files in there: test.py and main.py. Usually when working on a project, you want to import all the helper modules and run them in main.py . Anyway, open test.py and just put:

print('test.py says Hi!')

and open main.py and put:

import test # We do not need to put .py

Alright. Now run main.py and you should see "test.py says Hi!" printed.

More on import syntax

You can import certain modules in another way. If you do not want to type the whole time.sleep part, you can directly import sleep from time and use it as if it was defined in your file. We use the from/import keywords.

from time import sleep
print('Start')
sleep(5)
print('End')

Easy. We can also use aliases using the as keyword:

from time import sleep as delay
delay(2)

Libraries

A library is simply a bunch of modules laid out in an easy-to-use format for you. You import it just like a module, although in some cases you might need to:

import library.module
# OR
from library.module import function

You may hear something about namespaces. Since they're not strictly related to Python, I'll leave that for you to Google.

Third-party libraries and modules

OK, now you've learnt a lot about libraries and modules. The Standard Library has a lot of useful libraries and modules, and you'll almost always find one in there for your needs. But again, you'll almost always find a better/easier solution. The Python community has created modules and libraries for almost every single thing you might want to do in Python. You can install them easily, and I'll show you how in the next chapter.

Why use modules/libraries?

You might think, why use all these modules and libraries, can't I just make my own? Sure, you could. But there are hundreds and thousands of developers working on popular libraries, which means there were months or years of development time invested in those projects. You can simply not make them all by yourself. So, do yourself a favor, don't reinvent the wheel and use all those free libraries available to you.

Last updated