Skip to content

7.5 Import modules

There are several ways to import modules as we will explain in this topic. Please refer to section 7.1 - Distinguish between functions & modules to revise what are Python modules and why do we use them.

7.5a Import modules with import

This statement imports the entire module. To use any function or class from the module, you must prefix it with the module name.

Syntax:

import module_name

 

Example 7.5.1 - import ; imports the entire module, requiring you to use the module name prefix (random.) to access functions.

import random

# Generate a random integer between 1 and 10
random_integer = random.randint(1, 10)
print(f"Random integer between 1 and 10: {random_integer}")

Example 7.5.1 - Output

Random integer between 1 and 10: 3

 


 

7.5b Import modules with from ... import

This statement imports a specific function or class from the module. You can use the imported function or class directly without prefixing it with the module name.

Syntax:

from module_name import function_name

 

Example 7.5.2 - from ... import ; imports only the specific function randint, allowing you to use it directly without the module name prefix

from random import randint

# Generate a random integer between 1 and 10
random_integer = randint(1, 10)
print(f"Random integer between 1 and 10: {random_integer}")

Example 7.5.2 - Output

Random integer between 1 and 10: 7

 


 

7.5c Import modules with from ... import ... as

This statement imports a specific function or class from the module and gives it a different name (alias). You can use the alias name to refer to the imported function or class.

Syntax:

from module_name import function_name as alias_name

 

Example 7.5.3 - from ... import ... as ; import the randrange function from the random module and alias it as num_generator

from random import randrange as num_generator

# Generate a random integer between 1 and 10
random_integer = num_generator(1, 11)  # Note: randrange excludes the stop value
print(f"Random integer between 1 and 10: {random_integer}")

Example 7.5.3 - Output

Random integer between 1 and 10: 8

 


 

7.5d Import module with from ... import *

The from ... import * statement imports all the functions, classes, and variables from the stated module directly into your current namespace. This means you can use them without prefixing them with the module name.

Syntax:

from module_name import *

 

Example 7.5.4 Using from random import * ; In this example, we'll use various functions from the random module without needing to prefix them with random..

from random import *

# Generate a random integer between 1 and 10
random_integer = randint(1, 10)
print(f"Random integer between 1 and 10: {random_integer}")

# Generate a random floating-point number between 0 and 1
random_float = random()
print(f"Random floating-point number between 0 and 1: {random_float}")

Example 7.5.4 - Output

Random integer between 1 and 10: 1
Random floating-point number between 0 and 1: 0.6928575893172548

 


 

7.5e Summary of how you can import modules in Python

  • import module_name

    • Imports the entire module.
    • Use the module name to access functions/classes.
  • from module_name import function_name

    • Imports only the specified function or class.
    • Use the function/class directly without the module name.
  • from module_name import function_name as alias_name

    • Imports the specified function or class and assigns it an alias.
    • Use the alias name to refer to the function/class.
  • from module_name import *

    • Imports all functions, classes, and variables from the module directly into your namespace.
    • Simplifies function calls by removing the need for the module prefix.
    • Can lead to namespace conflicts and reduced code readability.
    • In practice, it's generally better to import only the specific functions you need or use a prefix to avoid issues.

 

Note

Understanding these different import statements will help you write more readable and efficient Python code by only importing what you need and avoiding potential naming conflicts.