Get startedGet started for free

Modules

1. Modules

We know how useful Python's built-in functions are. Now let's look at another helpful component of the Python ecosystem - modules.

2. What are modules?

Modules are Python scripts, meaning they are files ending with the .py extension. Modules contain functions and attributes - more on those later. They can also contain other modules! Python comes with several modules that are available for us to use. As with built-in functions, modules help us avoid writing code that already exists!

3. Python modules

There are around 200 built-in modules available to us! Popular modules include os, which can be used to interact with your operating system, such as finding the current working directory. There's the collections module, which offers a variety of advanced data structures. String is a module for performing string operations, while logging can be used to log information about a program during testing or production. There's also subprocess, which, among many things, enables us to run terminal commands from within a Python file. The full list of modules can be accessed from the link in the slide.

4. Importing a module

If we want to use code from a module then we need to import it. To do this, we use the import keyword followed by the name of the module. Here, we import the os module. Checking the type confirms that os is a module.

5. Finding a module's functions

We have the module imported, but how do we know what functions are available? The easiest approach is to look at the documentation - the URL is displayed in the slide. We can also use the help function, but be warned, this will return a large output and likely contain lots of information we don't need!

6. Getting the current working directory

We can use a function from the os module by writing os-dot, followed by the function name. For example, here we call the getcwd function, which retrieves the current working directory. The output shows the full directory name, including any directories above it such as username. Notice the quote marks, meaning the output is a string. This can be useful if we need to refer to the directory in our script, such as to read in multiple files from our directory. We assign the results of os.getcwd to a variable called work_dir.

7. Changing directory

We can also change directory using the os.chdir function, passing in the directory we want to move into. Calling os.getcwd again shows we have successfully moved. However, as we created a work_dir variable, we can see this variable's value remains unchanged!

8. Module attributes

The os module also has attributes, which are values, in contrast to functions, which perform a task. For instance, we can get information about our local environment using the os.environ attribute. Notice, as it's an attribute, we don't include parentheses afterward, as we aren't calling it like we would with a function. This returns a dictionary with values such as the location where Python is installed, and the timezone we are based in.

9. Importing a single function from a module

So far we've only used two os functions, but we've imported the entire module, which can take up a lot of memory. If we only need to use a specific function then we can adapt our import statement. To only get the chdir function, we write from os import chdir.

10. Importing multiple functions from a module

If we want more than one function then we can add a comma, followed by the additional function we require. With this approach, we can call the function without referring to the module first, because we haven't imported the entire module so Python won't know what os means.

11. Let's practice!

Now it's your turn to work with modules!