To import all functions, classes, and variables from a Python module, use the syntax from module_name import *. This is known as a "wildcard import," and it allows you to call every public object defined in that module directly by its name without needing to prefix it with the module name (e.g., using sqrt() instead of math.sqrt()). While this provides a shortcut for coding, it is generally discouraged in professional production environments because it can lead to namespace pollution and naming conflicts.
What You'll Need
- Python installed on your system (Python 3.x is recommended).
- A code editor or Integrated Development Environment (IDE) such as VS Code, PyCharm, or a simple text editor.
- A target module to import from, which can be a built-in Python library (like
mathorrandom) or a custom.pyfile located in your project directory.
Step-by-Step
- Identify the Module: Determine which module contains the tools you need. For this example, we will use the built-in
mathmodule, which contains mathematical constants and functions. - Write the Import Statement: At the very top of your Python script, type the following line:
from math import *. Thefromkeyword specifies the source module, and theimport *tells Python to grab every public attribute from that module and place it directly into your current script's local namespace. - Call Functions Directly: Instead of writing
math.piormath.sqrt(16), you can now use the names directly. For example, writeprint(pi)orprint(sqrt(16)). Python will look in the local scope first; if it doesn't find the name there, it will look through the objects imported via the asterisk. - Test with a Custom Module: To see how this works with your own files, create a file named
my_tools.py. Inside it, define a few variables and functions:def greet(): return "Hello from my tools!" version = 1.0
In a second file in the same folder, writefrom my_tools import *. You can now callgreet()and printversionwithout referencingmy_tools. - Verify the Namespace: If you are unsure what was actually imported, you can use the
dir()function. Callingprint(dir())after your wildcard import will show a list of all names currently available in your local scope, including everything pulled in from the module.
Common Mistakes
The most frequent mistake when using import * is creating naming collisions. This happens when two different modules have functions with the same name. For example, if you run from module_a import * and then from module_b import *, and both modules contain a function called calculate(), the second import will silently overwrite the first. Your code will use the version from module_b, which can lead to unpredictable bugs and logic errors that are extremely difficult to trace because there is no explicit error message.
Another common mistake is importing everything from a module when you only need one or two specific functions. This increases the memory footprint of your script and makes the code less readable for other developers. When a collaborator sees a function call like process_data() in a script that has five different import * statements at the top, they have to search through every single one of those modules to find where the function is actually defined.
Users also frequently forget that import * only imports "public" objects. In Python, any function or variable starting with a single underscore (e.g., _private_func()) is considered internal. These are not imported when using the asterisk, which can lead to NameError if you expect a "hidden" utility function to be available.
Tips
If you find yourself wanting to use import * to save keystrokes, consider using the import module as alias approach instead. For example, import pandas as pd allows you to use pd.DataFrame(), which is short but still tells anyone reading the code exactly where the DataFrame class comes from. This maintains the balance between brevity and clarity.
If you are creating your own module and want to control exactly what happens when someone uses from your_module import *, you can define a special list called __all__. By adding __all__ = ['func1', 'func2'] at the top of your module, you tell Python to only export those specific names when a wildcard import is used, hiding everything else. This is a best practice for library creators to prevent internal helper functions from leaking into the user's namespace.
For interactive sessions (like using the Python REPL or a Jupyter Notebook), import * is often acceptable because you are experimenting and speed is more important than long-term maintainability. However, always transition to explicit imports when moving that code into a .py file for production.
Troubleshooting
If you receive a ModuleNotFoundError, the first step is to check the spelling of the module name. Python is case-sensitive, so Math is not the same as math. If the module is a custom file, ensure that the file is in the same directory as the script you are running. If it is in a subfolder, you must use dot notation (e.g., from folder_name.module_name import *) and ensure the folder contains an __init__.py file (though this is less strict in newer Python 3 versions).
If you encounter a NameError: name 'x' is not defined despite using import *, check if the object you are trying to access starts with an underscore. As mentioned, private members are excluded from wildcard imports. You will need to import that specific name explicitly: from module import _private_func.
If your code is behaving strangely or returning incorrect values, check for shadowing. You may have imported a function via * that has the same name as a variable you defined later in your script. For instance, if you import a function called sum from a module and then create a variable sum = 10, the variable will "shadow" the function, and you will no longer be able to call the function. To fix this, rename your local variables to avoid overlapping with common module function names.
Frequently Asked Questions
Is using import * considered bad practice?
Yes, in most professional contexts. It obscures the origin of functions and can lead to naming conflicts that are hard to debug. Explicit imports (from module import func) are preferred.
Does import * slow down the execution of my program?
The actual time it takes to perform the import is negligible. However, it can slightly increase memory usage because you are adding many names to the local namespace that you may never actually use.
Can I use import * with multiple modules?
Yes, you can. However, if two modules have functions with the same name, the module imported last will overwrite the functions from the previous ones.
What is the difference between import module and from module import *?
import module requires you to use the prefix (module.function()), keeping the namespace clean. from module import * lets you use function() directly, but mixes the module's names into your own.
How do I stop certain functions from being imported with *?
In the module you are writing, define the __all__ list. Only the names included in that list will be exported during a wildcard import.