Understanding the Differences Between import and from ... import in Python

preview_player
Показать описание
Explore why `import` and `from ... import` behave differently in Python and how to correctly structure your imports in a package setup.
---

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Why do *import ...* and *from x import y* idioms behave so differently here?

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Why Do import ... and from x import y Behave Differently in Python?

Python is a powerful programming language that offers various ways to import modules and classes. However, newcomers often get confused by the differences between the import ... and from ... import ... idioms. In this guide, we’ll elucidate this topic, especially in the context of a specific package structure.

The Package Structure

Consider the following package structure you have set up:

[[See Video to Reveal this Text or Code Snippet]]

[[See Video to Reveal this Text or Code Snippet]]

The Problem

Option 1 - Using from ... import ...:

[[See Video to Reveal this Text or Code Snippet]]

Option 2 - Using import:

[[See Video to Reveal this Text or Code Snippet]]

While the first option works perfectly, the second option throws an error:

[[See Video to Reveal this Text or Code Snippet]]

Understanding Imports

To clarify the situation, it's essential to understand how imports function in Python:

The import x Statement

By simply importing module2, you’re not accessing SomeClassFromModule2 directly; instead, you need to create an instance of SomeClassFromModule2 using the dot notation that follows the module name.

The from x import y Statement

This allows you to instantiate SomeClassFromModule2 without the need for additional namespace qualification.

Why the Difference Matters

The difference between these two import methods boils down to how the symbols (classes, functions, etc.) are imported into the current namespace. Here’s a summary:

Importing Entire Module: When using import, get everything within the module but reference specifically through the module’s namespace.

Importing Specific Classes: Using from ... import ... brings in only the specified class or function directly into your namespace, allowing for more straightforward usage.

Conclusion

In summary, understanding the distinctions between import and from ... import ... is crucial when you are working with Python packages. Here’s a quick recap of the essential takeaways:

Use import to bring in the whole module while referencing specific classes and functions with their module names.

Use from ... import ... when you need to specify and simplify access to a particular class or function directly.

With these principles, you should be able to structure your imports effectively in Python, reducing confusion and runtime errors.

Happy coding!
Рекомендации по теме
welcome to shbcf.ru