Intro to Modules and Packages in Python
This course about modules and packages covers their purpose and how to create them. We also take a deeper look at a handful of common modules. This course is part of a series of content designed to help you learn to program with the Python programming language.
- Describe the concept of a module
- Describe the concept of a package
- Describe how to create a module
- Describe how to create a package
- Describe the purpose of sys.path
- Describe the purpose of the built-in dir function
- Describe how to import objects from modules
- Describe the purpose of the standard library
- Describe the purpose of the sys, os, math, collections, and random modules
This course was designed for first-time developers wanting to learn Python.
This is an introductory course and doesn’t require any prior programming knowledge.
Hello, and welcome! My name is Ben Lambert, and I’ll be your instructor for this course. This course is part of a series of content designed to help you learn to program with the Python programming language. Should you wish to ask me a specific question, you can do that with the contact details on screen.
You can also reach support by using the email address: firstname.lastname@example.org. And one of our cloud experts will reply. Built into the Python runtime is a mechanism for organizing code into groups based on the shared purpose of the code. This organizational mechanism of packages and modules is going to be the focus of this lesson.
Sometimes we as developers write code to solve a specific task. Imagine that once per week you’re required to download a file, check it for the existence of some data, and if that data meets some requirement you need to send an email. This is a rather specific task to you and your job - which probably isn’t generally useful to other developers. These types of tasks are commonly automated. If we were to automate this in Python we could create a single Python file containing all the code. And this code could be scheduled to run once per week. This type of self-contained single-task code is typically called a script. Scripts are a common means of automating specific tasks.
Scripts are common in certain job roles. For example: data engineers might create scripts for transforming data. And system administrators might create scripts that install software patches. Scripts are perhaps the simplest way to develop Python code because all of the code resides in one file. However, this poses a problem when we want to reuse code.
Imagine that while automating some task that you write a generally useful function that you could use in other Python code. If all of the code resides in a single script then to reuse that code you would need to copy and paste it when it’s needed. That might work once or twice however, it doesn’t really scale. As soon as you need to make a change to the code, then you’ll have to change it in multiple scripts.
The Python runtime includes a mechanism for grouping code based on its functionality. This mechanism is also built into the language syntax. This mechanism is referred to as modules. A module is a single Python file which contains reusable code. Code residing inside a module becomes accessible to us by importing it. This provides us access to the module’s objects from inside our own code file. Python’s standard library is organized into modules. Where each module contains object types related to the purpose of the module. There are modules used to interact with the operating system; modules used to interact with the Python runtime; modules for networking; modules for working with dates and times; and many others. Each module is built to serve some singular purpose.
Let’s look at Python’s datetime module as an example. Inside the datetime module there are object types used for working with dates and times. For example there are callables which will return today’s date; callables which can compare dates; and other code related to dates and times. If individual objects - such as functions and classes - are similar to lego blocks, then modules would be considered a full lego set. Because all of the objects contained in the module share the same purpose.
So, modules are a collection of reusable object types which share a purpose. Some concepts can be fully represented with a single module. However, some concepts are more complex and may require multiple modules. This is the problem that packages solve. A package is a collection of modules which all share a purpose.
Python’s urllib package is an example of a package. This package is used to send requests to URLs in the same way a web browser would. You can specify a URL and make a request. Doing this will attempt to connect to the server responsible for that URL and download the response. Looking at the different modules inside this package we can see that they all relate to some aspect of working with URLs. The request module is responsible for providing object types used for making requests and obtaining results.
So, modules are used to group code into reusable units based on a shared purpose. And packages are used to group modules into a reusable unit based on the shared purpose of its modules. Once code exists inside of a module we can import that code into our own code and use its object types to create new objects. Let’s use the urllib package to make a web request to a URL. For this lesson, don’t focus on the syntax used to import modules. We’ll cover that later. For now, I just want you to see the code in use.
The first line is responsible for importing a function from the urllib package and the request module. The function is called urlopen. This function accepts many optional input parameters; however, the only required parameter is the URL that we want to request. In this case the urlopen function will return an HTTPResponse object if the connection is successful.
When working with HTTP requests there are numeric status codes used to indicate the status of the response. A status of 200 indicates that the request was successful. You’re possibly familiar with the status of 404 - which indicates that the resource wasn’t found. If the request was successful, we can get the results from the response by calling the read method. This line re-binds the response name to the actual HTML that’s returned from the request. And finally the resulting HTML is printed to the console. Where it doesn’t look at all like it does once rendered by a browser.
This code and its purpose aren’t really important here. It’s the idea of importing objects that’s important. By importing the urlopen function from the request module of the urllib package, we were able to make a web request with just a few lines of code. This is the value of modules. When we have code that can be generally useful it can become a module. Which enables us to reuse code. When we have multiple modules used for the same shared purpose we can place them inside a package.
Okay, this seems like a natural stopping point. Here are your key takeaways for this lesson:
A module is a collection of reusable object types which share a purpose.
A package is a collection of modules which share a purpose.
For example the urllib package which contains multiple related modules.
That's all for this lesson. Thanks so much for watching. And I’ll see you in another lesson!
Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.