Blog Post #110: Bringing It All In: The import and from…import Statements

In Post #109, we created our first module, my_math_functions.py, and used the import my_math_functions statement to use its code in our main.py script. This is the most basic way to import, but Python offers several other ways to bring code into your project, each with its own advantages and disadvantages.

In this post, we’ll explore the different import syntaxes, focusing on the two main approaches—import module and from module import something—and discuss the pros and cons of each.

We will continue to use the my_math_functions.py module from our last post:

# my_math_functions.py
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

Method 1: import module

This is the method we’ve already seen. It imports the entire module as a single “object” or namespace. To access anything inside it, you must use the module_name. prefix.

# main.py
import my_math_functions

result = my_math_functions.add(10, 5)
print(result)
  • Pros:
    • Clarity: Every time you call a function, you use the module_name. prefix (e.g., my_math_functions.add()). This makes it perfectly clear where the function is coming from.
    • No Name Collisions: If you have your own function named add in main.py, it won’t conflict with my_math_functions.add() because they exist in different namespaces. This is the safest way to import.
  • Cons:
    • Verbosity: It can lead to longer lines of code if the module name is long or if you use its functions many times.

Method 2: from module import something

What if you only need one or two specific functions from a module and want to use them frequently? The from...import statement lets you import a specific name directly into your current script’s namespace.

# main.py
from my_math_functions import add

# Now we can call 'add' directly without the module prefix
result = add(10, 5)
print(result)

You can also import multiple names by separating them with a comma:

from my_math_functions import add, subtract

  • Pros:
    • Conciseness: Your code is less verbose because you don’t need the module prefix for every call (add() vs. my_math_functions.add()).
  • Cons:
    • Potential Name Collisions: This is the main drawback. If you import a function named add and later define your own function named add, the second definition will overwrite the imported one. This can lead to very confusing bugs.

Variations and Best Practices

Using an Alias with as

Both import styles allow you to use the as keyword to create an alias, or a shorter nickname. This is extremely useful for long module names.

# Give the module a shorter name
import my_math_functions as mmf

result = mmf.add(10, 5)
print(result)

You can also alias a specific function name.

from my_math_functions import subtract as sub

result = sub(10, 5)
print(result)

This is very common in the data science world (e.g., import pandas as pd).

The “Star” Import: from module import *

There is a syntax to import everything from a module directly into your namespace: from my_math_functions import *. This is called a “star import.”

Warning: While this might seem convenient, it is strongly discouraged in most production code. It clutters your script’s namespace with potentially dozens of names, makes it impossible to see where a function came from just by reading the code, and dramatically increases the risk of name collisions.

Summary: Which Should You Use?

  • import module: This should be your default choice. It’s the most explicit and safest from name collisions.
  • import module as alias: Use this when a module name is very long to make your code more readable.
  • from module import function: Use this when you need a few specific functions from a module and you will be using them frequently.
  • from module import *: Avoid this in your scripts. It’s only occasionally acceptable in temporary, interactive Python shells for quick exploration.

What’s Next?

You now have a full understanding of how to import code in Python. Choosing the right import style is a key part of writing clean, readable, and bug-free code.

There is one more piece of the puzzle. When you import a module, Python runs the entire file. What if there’s code in that file that you only want to run when the file is executed directly, and not when it’s imported as a module? In Post #111, we will demystify one of the most common and important lines you’ll see in Python scripts: if __name__ == '__main__'.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment