Resource linked in this lesson: Advanced Python import Techniques
Navigating Name and Import Errors
00:00
There’s a few different ways that accessing names in Python can trip you up. If you try to access a variable name that isn’t present in a given Python scope, you’ll encounter a NameError
.
00:10
On the other hand, if you try to import something and it can’t be found, you’ll either get a ModuleNotFoundError
or its more general parent class, the ImportError
.
00:20 Let’s see some situations that can trigger these exceptions.
00:24
Open up the REPL, and a NameError
is easy to generate. Try to access a variable name that doesn’t exist like bob
. Indeed. NameError
: name ‘bob’ is not defined`.
00:37
A more realistic example would be accessing a library that does exist that maybe you forgot to import, like trying to grab the path
attribute of the sys
module. sys.path
, and a much more helpful error this time. NameError
: name ‘sys’ is not defined`.
00:53
Did you forget to import 'sys'?
The ModuleNotFoundError
occurs when you ask Python to import a module and it can’t be found. This happens most commonly when you’ve forgotten to install the module prior to running the code.
01:06
Or perhaps you’ve misspelled the module’s name. Try to import a non-existing module named non_existing
. ModuleNotFoundError
: No module named ‘non_existing’.
01:18
Now this error only covers the cases where the module you’re importing or importing from can’t be found. All other cases of failed imports fall under the more general ImportError
.
01:28
An important distinction for you to consider, especially when it comes to handling these exceptions using try
except
. To see an ImportError
, you can try importing something that doesn’t exist from within a module that does.
01:41
from sys import non_existing
01:44
and there’s the different ImportError:
cannot import name 'nonexisting' from 'sys' (unknown location)
. For a practical example, you can use ModuleNotFoundError
to identify what a module is missing and dynamically load a different module at runtime.
02:00
If your program needs to parse a TOML file, for instance, you could use the built-in tomllib
module in any version of Python since 3.11. But if you try to import tomllib
and get a ModuleNotFoundError
, you can use try
and except
to instead import the third-party library tomli
, which would, of course, need to be installed as a dependency.
02:20
You could do something like this: try: import tomllib
02:28
except ModuleNotFoundError:
02:33
import tomli as tomllib
.
02:37
Either way, you end up with a TOML parsing library assigned to the variable named tomllib
. Of course, this only works if the two libraries have more or less the same API, but you get the general idea of what you can do with this.
02:51 And if these examples have made you a little more curious about how imports work generally, head on over to advanced Python import techniques for a deep dive.
03:00 It’s import-ant info for sure, and if you’re still with me after that pun, I’ll see you in the next lesson where you’ll look into lookup-related exceptions.
Become a Member to join the conversation.