Skip to content
Home / Fundamentals

Python Import Modules

In Python, a module is simply a Python file with a .py extension. For example, you might have a file called my_module.py that contains useful functions and variables that you want to use in your main program.

To create a module, simply create a new .py file and write your functions and variables. For example:

# my_module.py

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

num = 10

Import Statement

To use the functions and variables in your module in another program, you can use the import statement. Here is an example of how to import the entire module and use one of the functions:

# main.py

import my_module

result = my_module.add(5, 3)
print(result)  # prints 8

The import statement is used to import the entire my_module module. The module contains two functions, add and subtract, and a variable num.

The add function is then called using the dot notation (my_module.add)

From Import Statement

Alternatively to the import statement you can also perform imports using the from statement. Using the from keyword, you can import specific functions or variables from the module directly into the current namespace, like this:

# main.py

from my_module import add

result = add(5, 3)
print(result)  # prints 8

You can also import multiple functions or variables at once by separating them with commas:

# main.py

from my_module import add, subtract

result = add(5, 3)
print(result)  # prints 8

result = subtract(5, 3)
print(result)  # prints 2

The as Keyword

Sometimes you may want to import a module using a different name, either to make it easier to type or to avoid naming conflicts with other modules. To do this, you can use the as keyword in the import statement.

# main.py

import my_module as mm

result = mm.add(5, 3)
print(result)  # prints 8

This imports the my_module module under the name mm, so you can use the mm name to access the functions and variables in the module.

You can also use the as keyword when using the from import statement to give a specific function or variable a different name:

# main.py

from my_module import add as a

result = (5, 3)
print(result)  # prints 8

This imports the add function from my_module under the name a, so you can use the a name to call the function.

From * Import Statement

If you want to import all functions and variables from a module, you can use the from * syntax. This will import everything from the module into the current namespace, like this:

# main.py

from my_module import *

result = add(5, 3)
print(result)  # prints 8

result = subtract(5, 3)
print(result)  # prints 2

print(num)  # prints 10

Note that it is generally not a good idea to use the from * syntax, as it can make it difficult to determine where a particular function or variable is coming from.

Best Practices

  • If you use the from statement, import specific functions and variables instead of using the from * syntax. This makes it clear what is being imported and helps prevent naming conflicts.
  • Use the as keyword to give modules and functions descriptive names, to make your code easier to read and understand. This does mean that you have always to use the as keyword
  • If you are importing a module that contains a large number of functions or variables, consider using the import statement and accessing the functions and variables using the dot notation (e.g. module.function()). This makes it clear where each function and variable is coming from and helps prevent naming conflicts.