Python from…import Statement

Python, with its expansive library of modules and packages, offers a variety of functionalities. To access these functionalities in your code, you’ll often need to incorporate them using the import statement. In this article, we delve into the from…import variant of the import statement, which provides a more direct way to access specific functions, classes, or variables from modules and packages.

1. Basic import Statement

Before understanding from…import, let’s quickly recap the basic import statement:


import math
print(math.sqrt(25))

Here, we import the entire math module and then access its sqrt function using dot notation.

2. The from…import Statement

The from…import statement allows us to import specific attributes or functions from a module, so we don’t have to use dot notation.


from math import sqrt
print(sqrt(25))

Notice how we can now use the sqrt function directly without prefixing it with math..

3. Importing Multiple Attributes

You can import multiple attributes from a module by separating them with commas:


from math import sqrt, factorial
print(sqrt(25))
print(factorial(5))

4. Using as with from…import

The as keyword lets you assign a different name to the imported attribute, mainly to avoid name clashes or to provide a more readable alias:


from math import sqrt as square_root
print(square_root(25))

5. Importing All Attributes: *

It’s possible to import all attributes of a module using the * wildcard:


from math import *
print(sqrt(25))
print(factorial(5))

Caution: This method is generally discouraged, as it can make code less readable. You won’t know exactly which functions come from which module, especially if you’re importing from multiple modules. It can also lead to unintended name clashes.

6. Advantages of from…import

  • Direct Access: The chief advantage is the ability to directly access functions, reducing the need for dot notation and making the code somewhat cleaner.
  • Memory Efficiency: If you only need a few specific functions from a large module, importing just those can be more memory-efficient than importing the whole module.

7. Drawbacks of from…import

  • Name Clashes: If two modules have functions with the same name and you import both using from…import, you’ll overwrite the first one with the second one.
  • Reduced Readability: When you use the function in your code, it may not be immediately evident which module it comes from. This can be problematic in larger projects or when working with unfamiliar code.

8. Nested Imports with Packages

For larger projects structured as packages (with multiple modules), you can use nested from…import statements:

Suppose you have the following structure:


mypackage/
│
├── module1.py (contains a function named 'func1')
│
└── module2.py (contains a function named 'func2')

You can use:


from mypackage.module1 import func1
func1()

Conclusion

The from…import statement in Python offers a more concise way to import specific attributes or functions from a module or package. While it has its advantages in terms of direct access and memory efficiency, care must be taken to avoid issues like name clashes and reduced code clarity. As with many tools in programming, it’s essential to understand its use-cases and pitfalls to employ it effectively.

The post Python from…import Statement appeared first on TecAdmin.

General Articles Python Tips& Tricks TecAdmin