Explain Codes LogoExplain Codes Logo

Use 'import module' or 'from module import'?

python
best-practices
readability
namespace-conflicts
Anton ShumikhinbyAnton Shumikhin·Nov 17, 2024
TLDR

When you fancy to keep everything clear-cut and origin of methods or functionality transparent, go for import module:

import math # Round and round on the math merry-go-round until square root settles result = math.sqrt(4)

Jazz things up with good old from module import ... if you're going for a tad more brevity and you're giving a certain method a frequent flyer card:

from math import sqrt # sqrt doesn't mean square, right? result = sqrt(4)

Your decision here is a tango between clarity and brevity, and of course, the frequency of the airport runs (or the usage frequency) for your particular component.

Deep dive into the decisions behind imports

When you roll in the deep and decide between the whole module import or plucking specific attributes, it isn't code austerity for dummies; it's all about readability and ducking namespace conflicts. You can also marry the best of both worlds by aliasing through import module as mod — a sweet compromise of keeping context and reducing finger gymnastics on the keyboard.

Step into cleanliness — Namespace edition

Imagine from module import * as the aftermath of a 2-year-old’s birthday party — toys everywhere, misplaced items, and clutter galore. In essence, you pour out the content of a miscellaneous toolbox into your workspace. But if you cherry-pick what you need, your namespace is as clean and readable as a well-planned minimalist living room.

Designing namespaces and packages

When you're architecting your namespaces and planning your package layout, you're essentially pulling Marie Kondo into your project. A strategic layout not only sparks joy (and less conflicts!), but it also lends a helping hand to the readability and understanding of your project's structure.

Unraveling the true consequences of import styles

Always choose readability over brevity

"Readability counts", as stated in the Zen of Python. Hence, sometimes, even if it requires a few more moments of typing, choosing import module might enhance the code's readability and comprehensibility.

Dodging potential conflicts

Potential conflicts may lurk when dealing with third-party packages or large codebases. Strategically choosing from module import... and going with explicit imports can help you navigate this minefield.

Start-up performance — Food for thought

Our import statement choices do have an effect on the start-up performance of our Python script. Hold onto your hats folks, this may not be your primary concern, but it's good to know that every little helps when we're tweaking for performance.

Recap of best practices

  • Respect the Namespace: Keep your global namespace clean to easily understand your own code
  • Clarity and Maintenance: Weigh the convenience against the ability to read and update the code
  • Consistency is key: stay consistent with your import strategy
  • Align with Your Architecture: Reflect and support your codebase structure in your imports