Skip to main content

How to Import All Functions from a File in Python

This guide discusses how to import all functions (and other members, like variables and classes) from a Python file (module). While Python allows "wildcard" imports (from module import *), this practice is strongly discouraged due to its negative impact on code readability and maintainability. We'll explain the problems with wildcard imports and show the recommended, explicit ways to import what you need.

Wildcard Imports (from module import *) - Strongly Discouraged

You can import everything from a module using the * (wildcard) syntax:

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

def subtract(a, b):
return a - b
main.py
from another import *  # DON'T DO THIS!

print(multiply(5, 5)) # Output: 25
print(subtract(5, 2)) # Output: 3
  • The wildcard import from another import * makes the names defined inside the another.py directly available in the scope where the import happens.
  • You can specify the # pylint: disable=wildcard-import comment to disable the pylint warning.

Why Wildcard Imports Are Problematic

While seemingly convenient, wildcard imports (from module import *) cause several serious problems:

  • Namespace Pollution: They import everything from the module into your current namespace. This makes it much harder to know where a particular name is defined. If you import multiple modules this way, you can easily get name collisions (two modules defining the same name), leading to unexpected behavior.
  • Readability: It's unclear where functions, classes, or variables are coming from. Someone reading your code (including your future self!) has to manually inspect the imported module to understand what's available.
  • Maintainability: If the imported module changes (e.g., a new function is added with a name that conflicts with something in your code), your code can break unexpectedly.
  • IDE Support The code editors can not give suggestions for autocompletion, because it does not know what variables and methods the module has, when import * is used.

PEP 8, the official Python style guide, strongly discourages wildcard imports for these reasons.

The recommended way to import from a module is to be explicit about what you're importing. There are two main approaches:

Importing Specific Functions

Import only the specific functions, classes, or variables you need:

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

def subtract(a, b):
return a - b
main.py
from another import multiply, subtract

print(multiply(5, 5)) # Output: 25
print(subtract(5, 2)) # Output: 3
  • This is the best approach for most situations. It's clear what you're using from another.py.
  • It avoids name collisions.
  • It doesn't require any # pylint: disable=... comments, since it uses a clear style.

Importing the Entire Module

Import the module itself, and then access its members using dot notation:

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

def subtract(a, b):
return a - b
main.py
import another

print(another.multiply(5, 5)) # Output: 25
print(another.subtract(5, 2)) # Output: 3
  • This is also a good approach. It's slightly more verbose than importing specific names, but it clearly shows where each function comes from. It also avoids name collisions.
  • Modern IDEs can provide autocompletion after you type another., making this method very convenient.

You can also use this approach for multiple modules:

another.py
def multiply(a, b):
return a * b
def subtract(a, b):
return a - b
another2.py
def greet(name):
return f'hello {name}'

site = 'tutorialreference.com'
main.py
import another
import another2

print(another.multiply(5, 5)) # Output: 25
print(another.subtract(5, 2)) # Output: 3
print(another2.greet('Anna')) # Output: hello Anna
print(another2.site) # Output: tutorialreference.com