HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

12) Modules and Automation Lesson

Python Libraries, Modules and Packages

7 min to complete · By Martin Breuss

People have been writing code for a while, and they've solved a ton of recurring problems. They have built abstractions on top of abstractions to make it easier to tackle certain tasks. You can benefit from the work they did and use the code they wrote to make your own development process faster and more fun.

Colorful illustration of a light bulb

Info: The naming conventions can sometimes get a bit fuzzy and sloppy, and you'll see terms like module, package, and library used interchangeably. Precisely, a module is a single file containing Python code, and a package is a collection of modules. A library is a generic term for code that was designed to be used in other applications.

Python Standard Library

Python comes with batteries included, which means according to PEP 206:

having a rich and versatile standard library that is immediately available without making the user download separate packages.

The standard library is a collection of code that other people wrote, and that made its way right into the Python language. When you download and install Python, all of that code comes already included.

Python Modules

The code in Python's standard library is extremely useful, and it's often enough to accomplish whatever you want to do. However, in order to use a module from the standard library, you need to import it. This is so you can pick and choose what functionality you need and keep your script as small as possible.

Import Modules in Python

You can import a module from the standard library using an import statement. When you think back to your guess-the-number game, you imported Python's random module, which allowed you to create pseudorandom numbers. To import the random module, you just need to know its name:

import random

Putting this line of code at the top of your script imports the random module and gives you access to all the code that is defined there.

Modules Practice

  • Browse the docs for Python's standard library.
  • Pick two modules from the long list and try to import them into your script.
  • What small but interesting functionality can you build using one of these modules?

Import Statements

You can add an import statement at any point in your code, but it's good practice to add them all at the very top of your script. This has two reasons:

  1. The code of the module will only be accessible after the import statement
  2. It makes it easier to keep an overview of which modules are used in your script

Once you have imported a module, you are ready to use the code.

Code Namespaces

After importing a module using the import statement, you are ready to use the provided code in your script. Make sure to skim the documentation of the module to know what you can do with it. Many IDEs also provide code auto-completion, which can give you some insight into what code comes with the module.

To use the code you imported, you'll need to prepend it with the namespace of the module. For the random module, that namespace is random:

import random

 # A random number between 0 and 10
print(random.randint(0, 10))

Here, you're using the method .randint() from the random module to generate a pseudo-random number between 0 and 10.

The namespace is a way to let Python know that you want to access some code from the module you imported. By default, the namespace will always have the name of the module you imported.

There are some other ways you can import modules that you'll learn about later in the course, but you don't need to worry about them yet.

Colorful illustration of a light bulb

Additional Resources

Summary: Python Libraries, Modules and Packages

  • You can use code that other people wrote to speed up your development
  • A module is a single file containing Python code
  • A package is a collection of modules
  • A library is a generic term for code that was designed to be used in other applications
  • Python comes with a lot of code packaged in modules and packages inside of its standard library
  • import <name> is used to import a package or module with " "
  • The code of the module will be accessible anywhere below the import statement
  • Imported code can be accessed through the namespace of the imported code <name>.<code>
  • Module's documentation is the best way to learn the code inside of it