Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python import module

In Python, a module is a file containing Python definitions and statements. You can use modules to organize your code, reuse existing code, and share functionality across different scripts or applications. The import statement is used to load and access the functionality provided by a module.

In this tutorial, we'll cover the basics of importing modules in Python.

  • Importing a module:

To import a module, simply use the import statement followed by the module name. For example, let's import the built-in math module:

import math
  • Accessing functions and attributes from a module:

Once a module is imported, you can access its functions and attributes using the dot (.) notation. For example, let's use the sqrt() function and pi constant from the math module:

import math

result = math.sqrt(25)
print(result)  # Output: 5.0

area = math.pi * (5 ** 2)
print(area)  # Output: 78.53981633974483
  • Importing specific functions or attributes:

If you only need specific functions or attributes from a module, you can use the from keyword along with import. For example, let's import just the sqrt() function and pi constant from the math module:

from math import sqrt, pi

result = sqrt(25)
print(result)  # Output: 5.0

area = pi * (5 ** 2)
print(area)  # Output: 78.53981633974483
  • Renaming imports using as:

Sometimes, you may want to rename an imported module, function, or attribute to avoid naming conflicts or for better readability. You can use the as keyword to do so. For example, let's rename the sqrt() function from the math module:

from math import sqrt as square_root

result = square_root(25)
print(result)  # Output: 5.0
  • Importing all functions and attributes using *:

You can use the wildcard * to import all functions and attributes from a module. However, this practice is generally discouraged, as it can lead to naming conflicts and make it harder to understand which module a function or attribute comes from. Only use this method if you're sure that there are no naming conflicts.

from math import *

result = sqrt(25)
print(result)  # Output: 5.0

area = pi * (5 ** 2)
print(area)  # Output: 78.53981633974483
  • Importing your own module:

You can also create and import your own modules. Suppose you have a file named my_module.py in the same directory as your script:

# my_module.py

def greet(name):
    return f"Hello, {name}!"

To import and use the greet() function from my_module, simply use the import statement followed by the module name (without the .py extension):

import my_module

greeting = my_module.greet("John")
print(greeting)  # Output: Hello, John!

In summary, importing modules in Python allows you to reuse and share functionality across different scripts and applications. You can import built-in modules, third-party modules, or your own modules using the import statement. Access functions and attributes using the dot (.) notation or import specific functions and attributes using the from keyword. Rename imports using the as keyword.

  1. How to import a module in Python:

    • Description: Use the import keyword followed by the module name to import a module. Access module attributes using dot notation.
    • Code:
      import math
      
      result = math.sqrt(25)
      print(result)
      
  2. Importing specific functions from a module in Python:

    • Description: Import only the needed functions to reduce namespace clutter.
    • Code:
      from math import sqrt
      
      result = sqrt(25)
      print(result)
      
  3. Wildcard imports in Python for modules:

    • Description: Use * to import all functions and attributes from a module. Caution: It may lead to namespace pollution.
    • Code:
      from math import *
      
      result = sqrt(25)
      print(result)
      
  4. Importing modules from different directories in Python:

    • Description: Update sys.path or use relative imports to import modules from different directories.
    • Code:
      import sys
      sys.path.append('/path/to/module')
      
      import my_module
      
  5. Troubleshooting common import errors in Python:

    • Description: Common errors include module not found, circular imports, or incorrect usage of import statements.
    • Code: (example of a common error)
      # Incorrect import
      import non_existing_module
      
  6. Dynamic module import in Python:

    • Description: Use the importlib module to dynamically import modules based on user input or other runtime conditions.
    • Code:
      import importlib
      
      module_name = "math"
      math_module = importlib.import_module(module_name)
      result = math_module.sqrt(25)
      print(result)
      
  7. Circular imports in Python and how to avoid them:

    • Description: Circular imports occur when two or more modules depend on each other. Avoid by restructuring code or using import statements within functions.
    • Code: (example of avoiding circular import)
      # module_a.py
      def function_a():
          from module_b import function_b
          # Use function_b here
      
      # module_b.py
      def function_b():
          from module_a import function_a
          # Use function_a here