Loading video player…

Managing Imports With Python's __all__ (Overview)

Python has something called wildcard imports, which look like from module import *. This type of import allows you to quickly get all the objects from a module into your namespace. However, using this import on a package can be confusing because it’s not clear what you want to import: subpackages, modules, objects? Python has the __all__ variable to work around this issue.

The __all__ variable is a list of strings where each string represents the name of a variable, function, class, or module that you want to expose to wildcard imports.

In this video course, you’ll:

  • Understand wildcard imports in Python
  • Use __all__ to control the modules that you expose to wildcard imports
  • Control the names that you expose in modules and packages
  • Explore other use cases of the __all__ variable
  • Learn some benefits and best practices of using __all__

To get the most out of this video course, you should be familiar with a few Python concepts, including modules and packages, and the import system.

Resource mentioned in this lesson: Python Basics: Modules and Packages (Video Course)

Download

Course Slides (.pdf)

5.9 MB
Download

Sample Code (.zip)

7.1 KB

00:00 Oh, hi there. Welcome to “Managing Imports With Python’s __all__.” My name’s Joseph, and I’ll be your instructor for this video course. Importing objects is one of the first things you learn as a Python developer.

00:11 It lets you bring in code from other files to use in your programs, and it’s an essential part of building software. You might even know Python has a few different ways of importing objects. And one in particular, the wildcard import, lets you import everything from a package or module.

00:26 Right? That’s what I used to think, but that’s not quite right. In fact, the behavior of wildcard imports is a little more nuanced than that. It doesn’t actually import everything.

00:37 It varies between packages and modules, and there’s actually a special variable you can use to control its behavior further. Want to take a guess? Well, yeah, it’s in the title, __all__.

00:50 And just in case this is the first time you’re hearing the word dunder, you’re not mishearing me. It’s a contraction of double underscore, a common Python naming convention used for variables and methods with special meaning.

01:01 For example, __spam__ would be read as dunder spam. With that cleared up, what can you expect to learn in this course? The way wildcard imports work in Python, some of the differences between wildcard imports and explicit imports, how __all__ affects imports in modules versus packages, when you should use __all__ to customize import behavior, and some of the common use cases and best practices of using __all__.

01:28 As for prerequisites, you should be familiar with Python fundamentals, such as variables, functions, built-in data structures, and importantly, the concept of organizing code into modules and packages.

01:40 If that concept is still pretty new to you, check out the course in our Python Basics series on modules and packages. And in our next lesson, a review of how Python imports work.

01:51 See you there.

Become a Member to join the conversation.