Python > Modules and Packages > Modules > Importing Modules (`import` statement)

Basic Module Import

This snippet demonstrates the fundamental way to import a module in Python using the import statement. We'll use the built-in math module as an example. This module provides access to mathematical functions.

Code Example

This code imports the math module. After importing, you can access functions and constants within the module using the dot notation (module_name.function_name or module_name.constant_name). In this example, we use math.sqrt() to calculate the square root of 16 and math.pi to calculate the area of a circle.

import math

# Now you can use functions from the math module
result = math.sqrt(16)
print(result)  # Output: 4.0

circle_area = math.pi * 5**2
print(circle_area)

Concepts Behind the Snippet

The import statement makes the module available to your current script. Python searches for the module in a specific order: 1. The current directory. 2. Directories listed in the PYTHONPATH environment variable. 3. Installation-dependent default directory.

Real-Life Use Case Section

Imagine you're building a scientific application that requires complex mathematical calculations. Instead of writing these calculations from scratch, you can leverage the math module for functions like trigonometry, logarithms, and more. Similarly, if you need to work with dates and times, the datetime module provides a wealth of functionality.

Best Practices

  • Import at the Top: It's a general convention to import modules at the beginning of your Python script, making it clear what dependencies your code relies on.
  • Avoid Circular Imports: Be careful when importing modules that depend on each other, as this can lead to circular import errors. Consider refactoring your code to avoid such dependencies.

When to use them

Use the basic import statement when you need to access multiple functions or constants from a module and you want to keep the module's namespace clear (i.e., avoid name collisions).

Memory Footprint

Importing a module loads the entire module into memory. This can be a concern for very large modules, but for most standard modules, the impact is negligible. Selective imports (see the next example) can reduce memory usage if you only need a small part of a module.

Alternatives

Alternative ways to import are discussed in next example (selective import with from ... import).

Pros

  • Clarity: The code is easy to understand. It's clear which functions come from which modules.
  • Namespace Management: It avoids potential naming conflicts by requiring you to specify the module name when using a function or constant.
  • Cons

  • Verbosity: You have to use the module name every time you call a function or constant from that module.
  • FAQ

    • What happens if I try to import a module that doesn't exist?

      You'll get an ImportError. Double-check the module name and ensure it's installed in your Python environment.
    • Can I import a module multiple times?

      Yes, but Python only loads the module once. Subsequent import statements simply create new references to the same module object.