Import All Functions From File in Python

Import All Functions From File in Python

  1. Import All Functions From a File With the import * Statement in Python
  2. Reason to Not Use the import * Approach

This tutorial will discuss the method to import all the functions from a file in Python.

Import All Functions From a File With the import * Statement in Python

The import statement is used to import packages, modules, and libraries in our Python code.

We can use import * if we want to import everything from a file in our code. We have a file named functions.py that contains two functions square() and cube().

We can write from functions import * to import both functions in our code. We can then use both square() and cube() functions in our code.

The following code snippet shows a working implementation of this approach.

from functions import *

print(cube(3))

Output:

27

We imported all the functions inside the functions.py file inside our code with the import * statement in Python.

We then called the cube() function inside the functions.py file and printed the cube of 3. Although this method does the job, it is not advisable to use it.

Reason to Not Use the import * Approach

This approach uses an implicit import statement, whereas in Python we are always advised to use explicit import statements.

According to the zen of Python, “Explicit is better than implicit”. There are two key reasons for this statement.

The first reason is that it is very hard to understand which function is coming from which file as the size of the project increases, and we end up importing functions from multiple files. It is especially hard for someone else to read our code and fully understand what is happening.

It makes our code very hard to debug and maintain. This problem is highlighted in the following code snippet.

from functions import *
from functions1 import *
from functions2 import *
print(square(2))

Output:

4

In the above code snippet, it’s impossible to know where the original square() function is defined by just looking at the code. To fully understand the origins of the square() function, we have to explore all the files manually.

The second key reason is that if we have two functions with the same name in multiple files, the interpreter will use the most recent import statement. This phenomenon is demonstrated in the following code snippet.

from functions import *
print(hello())
from functions2 import *
print(hello())
print(hello())

Output:

hello from functions
hello from functions2
hello from functions2

The two files functions.py and functions2.py both contain a hello() function.

In the first line of output, we imported the functions.py file, and hence the hello() function inside this file is executed. In the second and third lines of output, we have also imported the functions2.py file, containing a hello() function.

So, the new hello() function is executed in the last two output lines.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Python Import

  • Python Circular Import
  • Python Import All Modules in One Directory
  • Import a Variable From Another File in Python
  • Import a Module From a Full File Path in Python
  • Import Modules From Parent Directory in Python