Python Modules and Packages: An Introduction (Overview)
In this course, you’ll explore Python modules and Python packages, two mechanisms that facilitate modular programming.
Modular programming is the process of breaking a large, unwieldy programming task into separate, smaller, more manageable subtasks or modules. Individual modules can then be put together like building blocks to create a larger application.
There are several advantages to modularizing code in a large application:
-
Simplicity: Rather than focusing on the entire problem at hand, a module typically focuses on one relatively small portion of the problem. If you’re working on a single module, then you’ll have a smaller problem domain to wrap your head around. This makes development easier and less error-prone.
-
Maintainability: Modules are typically designed so that they enforce logical boundaries between different problem domains. If modules are written in a way that minimizes interdependency, then there is decreased likelihood that modifications to a single module will have an impact on other parts of the program. (You may even be able to make changes to a module without having any knowledge of the application outside that module.) This makes it more viable for a team of many programmers to work collaboratively on a large application.
-
Reusability: Functionality defined in a single module can be easily reused (through an appropriately defined interface) by other parts of the application. This eliminates the need to recreate duplicate code.
-
Scoping: Modules typically define a separate namespace, which helps avoid collisions between identifiers in different areas of a program. (One of the tenets in the Zen of Python is Namespaces are one honking great idea—let’s do more of those!)
Functions, modules, and packages are all constructs in Python that promote code modularization.
If you’re just getting started with Python, then you might want to watch Python Basics: Modules and Packages before diving into this course.
00:00 Hi, I’m Chris Bailey from Real Python. I’m going to be taking you through the course Python Modules and Packages: an Introduction. Modules and packages facilitate modular programming.
00:12 Modular programming refers to the process of breaking a large, unwieldy programming task into separate, smaller, or more manageable subtasks or modules. And there’s several reasons for breaking apart those large programming tasks.
00:24 So, what are the advantages? One is simplicity. Rather than having to focus on the entire program at once, a module typically focuses on a relatively small portion of the problem.
00:34 If you’re working on a single module, you’ll have a smaller problem domain to wrap your head around. This is going to make development easier and less error-prone.
00:42 There’s also maintainability. Modules are typically designed so that they enforce logical boundaries between different types of problems or problem domains.
00:51 This makes it easy for a team of programmers to collaborate on a large application. And then there’s reusability. The functionality that you define in a single module can easily be reused throughout other parts of the application, or potentially reused in completely different applications.
01:07 This eliminates the need to recreate or duplicate code. Then, there’s scoping. Modules typically define a separate namespace, which avoids collisions between identifiers in different areas of your program.
01:18 You may have heard in the Zen of Python, “Namespaces are one honking great idea—let’s do more of those!” You’ll learn a lot about namespaces throughout this course.
01:27 Let me take you through the table of contents. It starts with this intro and course overview. Then you’re going to create a small module that you can practice writing and importing.
01:41
Then you’ll learn about the module search path. When you go to import a module, where will Python look for it? Then you’ll explore the import
statement and all the different forms that it can take.
02:02
You’ll learn about scope and namespaces, and learn about the dir()
function. Next, if you run a module as a script and execute it, what happens? And how to use the dunder attribute of __name__
.
02:21 In the next video, you’ll learn about reloading a module.
02:29 And the next video is about Python packages, and this is where, as your application has grown and you have many modules, how to group them together into a single package.
02:40 And then you’ll learn about package initialization.
02:48
After that, you’ll learn about importing using the wildcard (*
) from a package.
02:56 And to further your learning about how to organize your packaging, you’ll learn about subpackages.
03:06 Then there’ll be a conclusion and course review. Let me have you get started by writing a module.
Become a Member to join the conversation.