Advanced Python import Techniques (Overview)
In Python, you use the import
keyword to make code in one module available in another. Imports in Python are important for structuring your code effectively. Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable.
This video course will provide a thorough overview of Python’s import
statement and how it works. The import system is powerful, and you’ll learn how to harness this power. While you’ll cover many of the concepts behind Python’s import system, this video course is mostly example driven. You’ll learn from several code examples throughout.
In this video course, you’ll learn how to:
- Use modules, packages, and namespace packages
- Manage namespaces and avoid shadowing
- Avoid circular imports
- Import modules dynamically at runtime
- Customize Python’s import system
00:00 Welcome to Advanced Python import Techniques. My name is Christopher, and I will be your guide. This course is all about importing code into your program and the mechanisms Python has for managing groups of code.
00:13
This is a deep dive and you’ll cover a lot of topics, including all the variations of the import
statement, the differences between modules, packages, and namespace, how to create installable packages, what the import system does when you load a module, the problems with circular imports, how to dynamically load modules, and a way to bundle your code as a runnable zip file.
00:39
The code in this course was tested with Python 3.12. Much of it has been standard since Python 2, and so almost any version should work. The importlib.resources
library is covered, which got added in Python 3.7.
00:53 Anything after that should be good. Once your program reaches a certain size, you don’t want it all in one file. You want to organize it and keep similar things together.
01:04 In fact, even for small programs, it’s unusual not to at least import something from the standard library. That import thing is responsible for loading code from modules and packages, which are a hierarchical mechanism for grouping code together.
01:18
The import
keyword has three main variations, allowing you to load a module, load something from a module, and alias the loading thing as well.
01:28 And also, within those three mechanisms, there are variations, depending on relative and absolute specification of a module name.
01:35 Once you’ve got modules and packages, you’re likely to want to move them around so you can reuse them. Python has the concept of an installable package, or you can actually stick your code in a runnable zip file.
01:48 And if all that isn’t enough, you can dynamically control the loading of modules.
01:53 This course covers a lot. You’ll learn all the ins and outs of importing, where it can trip you up and likely some powerful aspects you haven’t seen before.
02:03 Next up, I’ll start out by going through all those import variations I just mentioned.
Become a Member to join the conversation.