from ... import
vs import .
If you're hunting for a specific tool (function or class) in a module, from ... import ...
is your lighten-the-burden method: from module import tool
. You can now use tool
directly. But, suppose you want the entire chest (module). In that case, import ...
requires explicit forwarding: import module
. You'd need to call at least module.tool
.
While from ... import ...
is the master of conciseness, import ...
has full context up its sleeves, helping to shake off name clashes and waving clarity's flag.
Why Choose One Over the Other (aka Advantages and Use Cases)
Keeping Built-ins Safe with Aliasing (and Friendly!👍)
Want some flexibility in your code without stepping on the toes of functions sharing the same name? Say hello to: from module import func as smiley_face
. Now everybody is smiling, including the built-ins who are kept safe and sound.
This approach ensures that your code doesn't accidentally step on built-in functions. This ensures compatibility and makes your code’s intent clearer.
Namespace Management 101 - Rule of Law in Python
Working with large-scale projects? The import jewel_box
let's you pull out a jewel using jewel_box.ruby
. This 'Naming Law' helps avert namespace conflicts and keeps your "imports" from crossing paths with local variables.
Turbo-Boost Your Performance
A negligible, but potential performance difference exists between from ... import ...
and import ...
. from .. import ..
can have a speed edge in computationally intensive operations. Since it doesn't need an attribute lookup, it saves precious nanoseconds! (Disclaimer: Don't let the illusion of speed get into your head too much!)
Practical Magic (Tips and Tricks)
Building Structured, Architectural Python Masterpieces
The import cathedral.spire
approach lets the gaze of awe-struck onlookers (aka future you and others reading your code) trace the tall module hierarchy unobstructed. Who knew Python could be so scenic?
Tackling The Giants - Working With Large Modules
Got large modules that could split the seams of your memory allocation? from mountain import molehill
only brings in what's needed, leaving the rest of the unused module up in the clouds.
Error Hero : Handling and Preventing Import Errors
You wouldn't want to trip over a misplaced submodule now, would you? Using from ... import ...
spots the road bumps (aka ImportErrors) early on so you can tackle them head on.
Advanced Insights for Aspiring Python Guru
The Doppelganger: Aliasing for Simplicity and Convention
The import ... as ...
syntax is an incognito spy, transforming keywords into aliases that improve code readability, code style, and align with PEP 8 standards of disguise.
Modular Living and Refactoring
Refactoring is a breeze when you make the right choices between from ... import ...
and import ...
. If you move a function between modules, import module
is your flexible friend, while from...
may need several pit stops for updates.
Dealing with Python Versions and Import Styles
Both import styles are well seen and accepted in Python 3. But in Python 2.x, the spy from module import submodule
was a necessary agent in some missions for relative imports thanks to some implicit behavioral tweaks.
Was this article helpful?