🛑 Overriding this function is strongly discouraged. To change the semantics of the
import statement, use import hooks instead!
Still here? 😉 So, let’s get started learning the syntax of this function. You can also check out our in-depth article on the
__import__ statement here.
__import__(name, globals=None, locals=None, fromlist=(), level=0)
|Import the module with this |
|Set the global namespace in which to search the |
|Set the local namespace in which to search the |
|Import the names of objects to be imported using this list argument.|
|Use absolute (|
We call this a “Dunder Method” for “Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.
Examples Overriding __import__
import my_module is semantically similar to the following:
my_module = __import__('my_module', globals(), locals(), , 0)
In case you need some background:
globals()returns a dictionary of
name --> objectmappings. The names are the ones defined globally, i.e., defined by Python or in the outside scope of your program. The objects are the values associated to these names.
locals()returns a dictionary of
name --> objectmappings. The names are the ones defined in the current local scope, i.e., defined within the current module, class, method, or function—whatever the most local scope is.
But what if you import an individual name (e.g.,
xyz) from the module
my_module? The call
import my_module.xyz gets translated to code similar to this one:
my_module = __import__('my_module.xyz', globals(), locals(), , 0)
💡 Note: The return value of the
__import__() dunder method is the top-level module, not the name xyz within it!
However, to import a module by name, the more Pythonic approach is
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.