Resource mentioned in this lesson: Python import: Advanced Techniques and Tips
Importing Objects in Python
00:00 Why do we need imports anyway? In modern software development, it’s unfeasible to build an application in a single file. Instead, programs are built from many pieces of code.
00:09 One application can span dozens, hundreds, even thousands of files. Projects become complex, often depending on many external libraries. By writing code in one place and running it elsewhere, you can reuse existing code to avoid duplication.
00:23 And with all of this internal and external code used and reused in multiple locations, imports provide a standardized way to locate and load code. In Python, the import system is just that.
00:37 It gives you access to code outside the current file. Using the built-in import statement, you can import code from the Python standard library, bundled with Python, or from third-party packages installed on your system, or even from your own project files.
00:52 You can break down Python imports into two main strategies. Explicit imports, where you import specific modules or objects by name, and wildcard imports, which by default import all public names from a module.
01:07
More on that later. First, let’s look at explicit imports. You can use an explicit import when you want to access a module or its contents by name. The basic syntax is import module with an optional as alias, which imports the module into the current namespace, the collection of named objects available to your code.
01:26 It only works if the module is available on Python’s import path, essentially checking if the module is in the working directory, installed as a third-party library, or is in the Python standard library.
01:37 And an optional alias can be provided, which is good for avoiding name collisions with other code objects in your file, or even just simplifying the name of the module you’re importing.
01:47 There are a few variants on this syntax.
01:50
import module, bringing the module’s name directly into your file’s namespace.
01:55
import module as alias, bringing it in under the alias you provide. from module import name is used to import specific names from the module, and from module import name as alias allows you to alias or rename a specific name you’re importing.
02:10 If you really want to dive deep into imports, import syntax, name resolution, and many more topics beyond the scope of this course, have a read through “Python import: Advanced Techniques and Tips.” Now let’s take a look at a couple quick examples of imports in practice.
02:26
Here we have a module, calculations.py, which you can find in the course resources. It has a few basic math operations, add, subtract, multiply, and divide.
02:36
All totally unnecessary given Python provides them, but it works for a demo. Open up a REPL session and make sure you’re in the same directory as calculations.py.
02:46
You can examine the names in the current global namespace by calling the built-in dir() function. You’ll see a bunch of dunder attributes, all standard stuff.
02:55
So when you import calculations, and call dir() again, there you see at the end calculations is added. To use the functions defined in calculations, you can use dot notation, like calculations.add(6, 7), returns 13.
03:14
And calculations.multiply(6, 7), returns 42. Since calculations is a lot to type, you could also use an alias. Clear. import calculations as calc.
03:28 And it behaves just the same.
03:32
calc.subtract(20, 7), returns 13, as you’d expect. And say you don’t need the whole module. You can also import a single object.
03:43
from calculations import divide, which becomes available directly.
03:52 which also returns 13. How about that? And for our purposes, that’s all you need to know about explicit imports. Next up, let’s talk about wildcard imports.
Become a Member to join the conversation.