You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python从子目录批量导入模块时import *失效的解决方案咨询

How to Batch Import All Modules from a Folder Without Listing Each One?

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__.py file inside folder1 (if it doesn’t exist already).

  • Add this code to the __init__.py file:

    # 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__.py approach 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 22:12:46