Python从子目录批量导入模块时import *失效的解决方案咨询
My Project Setup & Issue
I have the following project structure:
main_folder ├── main.py ├── folder1 │ ├── function1.py │ ├── function2.py │ └── ... │ └── function20.py
I’m trying to import all modules from folder1 into main.py. When I use from folder1 import *, nothing gets imported successfully—but if I specify individual modules like from folder1 import function1, it works fine. Since there are 20 scripts in folder1, I don’t want to write out every single import line manually. Is there a way to replicate the import * behavior for all modules in the folder?
Solutions to Try
Great question—this is a common pain point when working with larger Python package structures. Here are two reliable ways to handle this:
1. Use __init__.py to Enable import *
Python treats a folder as a package only if it contains an __init__.py file. By default, from package import * doesn’t import all submodules—you need to explicitly tell Python which modules to include.
Here’s how to set this up:
First, create an
__init__.pyfile insidefolder1(if it doesn’t exist already).Add this code to the
__init__.pyfile:# folder1/__init__.py import os from importlib import import_module # Get all .py files (exclude __init__.py itself) module_names = [f[:-3] for f in os.listdir(os.path.dirname(__file__)) if f.endswith('.py') and f != '__init__.py'] # Import each module and add to __all__ __all__ = module_names for name in module_names: import_module(f'.{name}', package=__name__)If you prefer a static approach (and don’t mind updating the list when adding new modules), you can hardcode the module names in
__all__:# folder1/__init__.py __all__ = ['function1', 'function2', ..., 'function20'] from . import *
Once this is set up, running from folder1 import * in main.py will import all 20 modules into your current namespace.
2. Dynamic Import Directly in main.py
If you don’t want to modify the folder1 package’s __init__.py, you can handle the dynamic import directly in main.py:
# main.py import os import sys from importlib import import_module # Add folder1 to the Python path folder1_dir = os.path.join(os.path.dirname(__file__), 'folder1') sys.path.append(folder1_dir) # Get all module files in folder1 module_files = [f[:-3] for f in os.listdir(folder1_dir) if f.endswith('.py') and f != '__init__.py'] # Import each module into the global scope for module_name in module_files: module = import_module(module_name) globals()[module_name] = module
After running this, you’ll be able to use function1.my_function() just like you would with a standard import.
Quick Notes
- The
__init__.pyapproach is more idiomatic for Python—it follows package conventions and makes your code easier for other developers to understand. - Dynamic imports (the second method) will automatically pick up new modules added to
folder1, but can make code harder to debug since imports aren’t explicit. Choose based on whether you need auto-detection or prefer explicit control.
内容的提问来源于stack exchange,提问作者raffa

