Last updated: Apr 11, 2024
Reading timeยท4 min
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.
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
.
from another import * print(multiply(5, 5)) # ๐๏ธ 25 print(subtract(5, 2)) # ๐๏ธ 3
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.
# 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.
def greet(name): return f'hello {name}' site = 'bobbyhadz.com'
You can import all functions/variables from the module as follows into your
main.py
file.
# 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
Notice that this approach also imports all variables from the specified file, not only the functions.
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.
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.
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
.
from another import multiply, subtract print(multiply(5, 5)) # ๐๏ธ 25 print(subtract(5, 2)) # ๐๏ธ 3
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.
def greet(name): return f'hello {name}' site = 'bobbyhadz.com'
Here is how you can import the function and variable explicitly.
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.
An alternative approach is to import the entire module and access the functions it defines as attributes.
Suppose that you have the following another.py
module.
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.
import another print(another.multiply(5, 5)) # ๐๏ธ 25 print(another.subtract(5, 2)) # ๐๏ธ 3
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.
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
.
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
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.
You can learn more about the related topics by checking out the following tutorials: