Python provides two common ways to import modules:importandfrom ... importThese two methods have significant differences in usage, namespace management, readability, and potential issues. These aspects will be explained in detail below.

1. Usage

import module:

  • Imports the entire module.
  • When used, objects in the module must be accessed via the module name.

Example

import math

result = math.sqrt(16)
print(result)  # Output 4.0

from module import name:

  • Imports specific objects (functions, classes, variables, etc.) from a module.
  • When used, directly reference the imported object without the module name prefix.

Example

from math import sqrt

result = sqrt(16)
print(result)  # Output 4.0

2. Namespace management

import module:

  • Imports the entire module as an object into the current namespace.
  • Objects in the module are accessed throughmodule.nameaccess, and will not conflict with other objects in the current namespace.

from module import name:

  • Imports objects from the module directly into the current namespace.
  • May cause naming conflicts with other objects in the current namespace.

Example

from math import sqrt
from cmath import sqrt  # sqrt for complex numbers

result = sqrt(-1)  # The sqrt function used may be confusing
print(result)

3. Readability and maintainability

import module:

  • In the code, it is clear which objects come from which module because the module name prefix is always used.
  • Improves code readability and maintainability.

Example

import os

current_directory = os.getcwd()
print(current_directory)  # Outputs the current working directory

from module import name:

  • In the code, object names are used directly, making it difficult to see which module those objects come from.
  • More comments or documentation are needed to keep the code clear.

Example

from os import getcwd

current_directory = getcwd()
print(current_directory)  # Outputs the current working directory

4. Selective import and memory usage

import module:

  • Imports the entire module, so all objects in the module are available.
  • Suitable when multiple objects from the module are needed.

from module import name:

  • Imports only specific objects from the module, saving memory.
  • Suitable when only a few objects from the module are needed.

Example

# Only need to use the date object from datetime
from datetime import date

today = date.today()
print(today)  # Outputs the current date

5. Importing all objects

from module import *:

  • Imports all objects from the module into the current namespace.
  • May cause naming conflicts, reduce code readability, and is not recommended.

Example

from math import *

print(sqrt(16))  # Directly use the sqrt function

Potential issues:

Example

from math import *
from cmath import *

result = sqrt(-1)  # It is unclear which module's sqrt function is used
print(result)

Summary

  • import module: Suitable when the entire module is needed, avoids naming conflicts, and improves code readability.
  • from module import name: Suitable when only some objects from the module are needed, simplifies code, but watch out for naming conflicts.
  • from module import *: Avoid using as much as possible; it may cause naming conflicts and code confusion.

Choosing the appropriate import method depends on the specific use case and requirements. It is generally recommended to use theimportstatement when needing to avoid naming conflicts and keep the code clear, and use thefrom ... importstatement when needing to simplify the code and use specific objects.