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

Advanced Python import Techniques (Summary)

In this video course, you’ve gotten to know the Python import system. Like many things in Python, it’s fairly straightforward to use for basic tasks like importing modules and packages. At the same time, the import system is quite complex, flexible, and extendable. You’ve learned several import-related tricks that you can take advantage of in your own code.

In this video course, you’ve learned how to:

  • Create namespace packages
  • Import resources and data files
  • Decide what to import dynamically at runtime
  • Extend Python’s import system
  • Handle different versions of packages

Throughout the video course, you’ve seen many links to further info. The most authoritative source on the Python import system is the official documentation:

You can put your knowledge of Python imports to use by following along with the examples in this video course. Click the link below for access to the source code:

Download

Sample Code (.zip)

20.3 KB
Download

Course Slides (.pdf)

1.9 MB

00:00 In the previous lesson, I showed you several import tips and tricks. This is the final lesson. I’m going to summarize the course and point you at some more resources.

00:10 This course has been all about importing modules and packages into your Python programs. Modules and packages are ways of organizing your code and with apologies to Steven Wright you can’t have everything in your namespace.

00:22 Where would you put it? The namespace is where all references live, and you use the import keyword to bring external things into the namespace, including modules or objects inside those modules. A module corresponds to a single file, and if you want more structure than that, you use a directory.

00:40 By putting a __init__.py file in a directory, you are creating a package. That __init__.py file is still a Python file, so you can use it to write side effect code, including importing other things into the namespace.

00:54 You can replace an object in the namespace by bringing in other ones with the same name or creating variables that overload the name. When you do this, you shadow the original value.

01:05 If you shadow a built-in, you can recover it by deleting the redefined value.

01:10 Shadowing also affects module loading. If you have a module with the same name as a built-in one, most of the time your module shadows the original. There are cases though, where this doesn’t happen depending on how the thing being shadowed is defined.

01:24 Once you’ve got a package, you can bundle it up for re-use. To do so, you typically create a source directory for your code then add a pyproject.toml file to describe your bundle.

01:35 These kinds of bundles are what gets put up on PyPI, but you can use your own locally by installing it with the -e option to pip install.

01:45 An absolute import is one with a fully qualified name. You can also import things relative to the current file. This is typically done with the dot shortcut.

01:55 Relative imports aren’t recommended though. If you move your file, the import breaks. It also won’t work if you package up your code in an installable bundle or put it in a Python zip archive.

02:06 When you first import a module, Python caches it in the sys.modules dictionary. Subsequent loads use the cache version, including if you’ve changed the file.

02:17 You can force a reload though by using the reload function in the importlib tools module. if you need to. importlib also has mechanisms for dynamically loading modules and resources from packages, and you can bundle up your scripts in a zip file if you wish naming it with a pyz extension and Python can run it from there directly.

02:39 This course is based on a very detailed tutorial on the Web and as much content as got covered here, the tutorial has even more. To keep things brief, I’ve used toy problems to show you how things work.

02:51 The tutorial took a different approach. It uses realistic programs, including one that uses namespace packages to define interfaces.

03:00 There are two different examples on loading resources, including using images in a package for a Tkinter program. There are also several examples on dealing with plugins, including dynamically loading them, and one program takes advantage of the module cache to create a singleton.

03:17 The tutorial also has one more topic that I didn’t cover. In Python, you can create customized handlers for finding modules as well as loading them. This allows you to do interesting things like import automatically from PyPI.

03:31 The tutorial has details on how you do this. There are also articles and tutorials on topics that I only touched briefly, including this one on all the steps involved in creating a package for PyPI.

03:44 If you’re interested in implicit namespace packages, this tutorial can give you more information.

03:50 The importlib module also provides information about a module or packages metadata. This feature got added in Python 3.8, so this course and tutorial on new features in 3.8 have sections on this topic.

04:04 And finally, if you’d like to learn more about using zip files to bundle code, this tutorial can help you out.

04:11 That’s all for this course. I hope you enjoyed it. Thanks for your attention.

Become a Member to join the conversation.