How to import all Functions from a File in Python

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# How to import all Functions from a File in Python

You can use a wildcard import to import all functions from a file in Python.

The wildcard import will import all of the members that are defined in the specified module which makes them directly accessible.

Suppose that you have the following another.py module.

another.py
def multiply(a, b): return a * b def subtract(a, b): return a - b

Here is an example of importing all functions from the module in a module named main.py.

main.py
from another import * print(multiply(5, 5)) # ๐Ÿ‘‰๏ธ 25 print(subtract(5, 2)) # ๐Ÿ‘‰๏ธ 3

import all functions from file in python

The code for this article is available on GitHub

Notice that we can directly access the functions that are defined in another.py in main.py.

If you use Pylint, you might have to disable the wildcard-import warning.

main.py
# pylint: disable=wildcard-import from another import * print(multiply(5, 5)) # ๐Ÿ‘‰๏ธ 25 print(subtract(5, 2)) # ๐Ÿ‘‰๏ธ 3

You can use the same approach if you need to import all functions from another module.

Suppose you have the following another2.py file in the same directory as your main.py and another.py files.

another2.py
def greet(name): return f'hello {name}' site = 'bobbyhadz.com'
The code for this article is available on GitHub

You can import all functions/variables from the module as follows into your main.py file.

main.py
# pylint: disable=wildcard-import from another import * # pylint: disable=wildcard-import from another2 import * print(multiply(5, 5)) # ๐Ÿ‘‰๏ธ 25 print(subtract(5, 2)) # ๐Ÿ‘‰๏ธ 3 print(greet('Bobby')) # ๐Ÿ‘‰๏ธ hello Bobby print(site) # ๐Ÿ‘‰๏ธ bobbyhadz.com

import all functions from multiple files in python

Notice that this approach also imports all variables from the specified file, not only the functions.

# Being explicit is better than being implicit

However, in most cases, this is not recommended because being explicit is better than being implicit.

When you import all functions and variables from a file, you pollute the global scope.

It is also more difficult to know what functions, classes and variables you can access from the other module without opening the module and checking what functions are defined.

This is the main reason we had to disable the Pylint wildcard-import rule.

An alternative approach is to explicitly import the functions you need to use in the module.

Suppose that you have the following another.py module.

another.py
def multiply(a, b): return a * b def subtract(a, b): return a - b

Here is an example of explicitly importing the two functions in a module called main.py.

main.py
from another import multiply, subtract print(multiply(5, 5)) # ๐Ÿ‘‰๏ธ 25 print(subtract(5, 2)) # ๐Ÿ‘‰๏ธ 3

explicitly importing the functions you need to use

The code for this article is available on GitHub

The code sample explicitly imports the multiply and subtract functions.

When using this approach, you know what functions you can use in main.py based on your import statement.

This also has the added advantage that you don't pollute the global scope with functions you don't need to use.

Suppose you have the following another2.py file.

another2.py
def greet(name): return f'hello {name}' site = 'bobbyhadz.com'

Here is how you can import the function and variable explicitly.

main.py
from another import multiply, subtract from another2 import greet, site print(multiply(5, 5)) # ๐Ÿ‘‰๏ธ 25 print(subtract(5, 2)) # ๐Ÿ‘‰๏ธ 3 print(greet('Bobby')) # ๐Ÿ‘‰๏ธ hello Bobby print(site) # ๐Ÿ‘‰๏ธ bobbyhadz.com

When using this approach, we don't have to add Pylint comments above each import statement.

Being explicit with what you need to import from a file is the recommended approach when writing Python code.

# Importing the entire module and accessing the functions as attributes

An alternative approach is to import the entire module and access the functions it defines as attributes.

This is better than using a wildcard import because your IDE can assist you with autocomplete when you type a dot after the module's name.

Suppose that you have the following another.py module.

another.py
def multiply(a, b): return a * b def subtract(a, b): return a - b

Here is how you can import the entire module in your main.py file.

main.py
import another print(another.multiply(5, 5)) # ๐Ÿ‘‰๏ธ 25 print(another.subtract(5, 2)) # ๐Ÿ‘‰๏ธ 3

import entire module and access functions as attributes

The code for this article is available on GitHub

We import the entire another module and access the multiply and subtract functions as attributes on the module object.

Once you type another., your IDE will assist you with autocomplete.

You also aren't polluting the global scope of main.py with all of the variables and functions that are defined in another.py.

Suppose you have the following another2.py file.

another2.py
def greet(name): return f'hello {name}' site = 'bobbyhadz.com'

The same approach can be used to import the functions and variables of another2.py into main.py.

main.py
import another import another2 print(another.multiply(5, 5)) # ๐Ÿ‘‰๏ธ 25 print(another.subtract(5, 2)) # ๐Ÿ‘‰๏ธ 3 print(another2.greet('bobby')) # ๐Ÿ‘‰๏ธ hello bobby print(another2.site) # ๐Ÿ‘‰๏ธ bobbyhadz.com

access functions as attributes

The code for this article is available on GitHub

Once you type another2., your code editor will show a dropdown list of the available attributes.

This is much better than having to open the another2.py file to check which functions, variables and classes it defines.

In general, when writing code, it is much better to be explicit than implicit because it makes your code more readable and intuitive and saves you time.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev