What is the Use of "from...import" Statement in Python?

One useful feature is the from... import statement, which lets you import attributes or functions from a module into your current namespace only. It offers a more accurate method of controlling what you add to your code, maintaining its efficiency and cleanliness.

Basic Syntax

You can import individual properties (functions, classes, or variables) from a module into your current namespace only by using the from... import statement. The fundamental syntax is:

Syntax:

1. Basic Import (import X)

  • When you use import X, Python imports the whole module X and establishes a reference to it in your current namespace.
  • name or X.attribute are examples of the full module path that must be used in order to access attributes or methods from X.

2. Selective Import (from X import ...)

  • You can import certain attributes straight into your namespace by using the from X import... command.
  • It implies that a simple (unqualified) name can refer to objects declared in module X.
  • But X.name won't work because the module X itself isn't declared in your namespace.

Example: Using datetime Module

Code:

Output:

2024-06-04 21:44:25.887277
  • In this example, we import only the datetime class from the datetime module.
  • Now we can directly use datetime.now() without specifying the full module path

Best Practices

Use from ... import when:

  • You want to save yourself from typing the module name repeatedly.
  • You're referring to a member of the module multiple times in your code.

Use import when:

  • It would help if you used multiple members from the same module

Remember, the choice between import and from ... import depends on your specific needs and coding style. Both have their place in Python development

Advantages of Selective Import

1. Clarity and Efficiency:

By importing only the necessary parts, you can prevent unnecessary attributes from cluttering your namespace and maintain clean code.

2. Shorter Code:

You can use unqualified names directly without the need for the module prefix.

Example 1: Importing a Single Attribute

Suppose we want to use the sqrt function from the math module:

Code:

Output:

Square root of 25: 5.0

In this example, we import only the sqrt function, making it accessible without the math. Prefix.

Example 2: Importing Multiple Attributes

Let's say we need both the sin and cos functions from the math module:

Code:

Output:

sin(30°): -0.99, cos(30°): 0.15

Here, we import both sin and cos functions for trigonometric calculations.

Importing Custom Functions from Your Module

Imagine you have a custom module named my_utils.py with a function called calculate_average. You can import it as follows:

Code:

Output:

Average score: 87.60

Here, we selectively import our custom function for calculating averages.

Importing Classes from a Module

If you have a module with a class definition, you can import the class directly:

Syntax:

Advanced Techniques:

1. Absolute vs. Relative Imports:

  • Importing modules with their complete path is referred to as an absolute import (e.g., import math).
  • Packages can refer to other modules within the same package by using relative imports (from. import my_module, for example).

2. Namespace Packages:

  • Namespace packages allow splitting a package across multiple directories.
  • Useful for large projects with modular organization.

3. Dynamic Imports:

  • Import modules at runtime based on conditions (e.g., user input or configuration).
  • Achieved using importlib.

4. Resource Imports:

  • Load data files (e.g., icons, configuration files) from your package.
  • Utilize importlib.resources.

5. Customizing Import Behavior:

  • Modify Python's import system behavior using hooks, finders, and loaders.
  • Useful for creating custom import mechanisms.

Remember that Python has a flexible import system that can help you develop more effective and maintainable code.

Python's from... import statement lets you import functions or objects from a module only. When you want to avoid typing the module name again, it's really helpful. To directly access the request function, for instance, use the urllib import request. Importing urllib.request accomplishes the same thing but necessitates using the entire module path. Aliasing imported functions is another way to prevent conflicts with built-in names. To sum up, the from... import command simplifies code by permitting accurate imports, improving legibility, and avoiding naming conflicts.