Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Python Packages

In this lesson, you’ll learn about Python packages. Suppose you have developed a very large application that includes many modules. As the number of modules grows, it becomes difficult to keep track of them all if they’re dumped in one location. This is particularly true if they have similar names or functionality. You might want a way to group and organize them.

Packages allow for a hierarchical structuring of the module namespace using dot notation. In the same way that modules help avoid collisions between global variable names, packages help avoid collisions between module names.

Creating a package is quite straightforward, since it makes use of the operating system’s inherent hierarchical file structure. Consider the following arrangement:

Image of a Python package

Here, there is a directory named pkg that contains two modules, mod1.py and mod2.py. Take a look at the contents of the modules. Here’s mod1.py:

Language: Python
def load_data():
    print('loading data using mod1.load_data()')

class Customer:
    pass

Here’s mod2.py:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Already a member? Sign-In

00:00 Up to now, you’ve only had a single module to import. But what happens as you start to build a much larger application? Well, that’s where Python packages can come in.

00:10 When you’re trying to keep track of that growing number of modules, creating a package is typically the solution. Packages allow for a hierarchical structuring of the module namespace, again using dot notation.

00:23 So, here’s an example package structure, and you’ll get to play with this in a moment. At the top level, you again have a directory, and let’s say that directory is named pkg (package), and inside of it, it includes mod1.py, and then it has a second module, mod2.py.

00:40 So now, both of these modules are inside of the directory hierarchy of pkg/. Well, how do you import them and work with those modules? Let me have you practice.

00:51 Inside your directory ModulesAndPackages/, I want you to create a new directory pkg.

00:59 So here’s the directory pkg/ and inside of it, you’re going to create two modules, mod1

01:08 and mod2. So mod1

01:16 is going to have a function called load_data(). load_data() when called is going to print out a line of text about the module.

01:38 It also has a class. This class is going to be the Customer class. Okay, so there’s a function named load_data() that’s part of mod1, and then there’s a class called Customer.

01:48 Again, just examples. mod2 is going to have a function named clean_data(). And again, right now it’s just going to be really simple.

02:08 mod2 will have a class also. This class will be the Location class.

02:15 So mod1—and you can see it here with load_data() and Customer. mod2 with clean_data() and Location.

02:21 Great. Now, you’re still in the root directory of ModulesAndPackages/, so if you were to start your REPL session,

02:32 what are the ways that you would import packages or modules from packages? Well, you can use dot notation. In fact, you could import both of them at once, so import pkg.mod1, pkg.mod2.

02:48 And what you’ll see here is that under pkg, both of the modules are available and under mod1, there’s the load_data() function, and you could see it running there.

03:00 And you could create an object, say x = pkg.mod2, and x is going to be a Location. Marking the spot, if you will. Great!

03:10