Python3中使用importlib动态导入模块报错问题咨询
importlib.import_module() and Package Parameters Let’s break down this confusion step by step—your issue boils down to how Python handles absolute vs. relative imports, and what the package parameter actually does (it’s not a directory path shortcut, which is probably where you went off track!).
Key Background: Absolute vs. Relative Imports
Python’s import system makes a critical distinction between two types of module names:
- Absolute names: No leading dots (e.g.,
'hello','folder.hello'). These are resolved against the directories insys.path(which includes your script’s root directory by default when you runmain.pydirectly). - Relative names: Start with one or more dots (e.g.,
'.hello','..utils'). These are resolved relative to a parent package, and that’s exactly what thepackageparameter is designed for.
Why Your Third Case Fails
Let’s unpack the problematic lines one by one:
1. importlib.import_module('hello', package='folder')
Here, 'hello' is an absolute module name (no leading dots). Python ignores the package parameter entirely when given an absolute name—it still searches for a top-level hello module in sys.path. Wait, but you have a top-level hello.py, so why the error?
The core issue is you’re trying to use a relative import pattern with an absolute module name. To target the hello.py inside folder using the package parameter, you need to use a relative module name (with a leading dot):
hello = importlib.import_module('.hello', package='folder')
The leading dot tells Python: "Look for a module named hello relative to the folder package."
2. importlib.import_module('hello', package='./folder')
This fails for a simpler reason: the package parameter expects a valid Python package name (like 'folder'), not a file system path (like './folder'). Python doesn’t recognize relative path syntax (slashes, ./) as a valid module name—package names use dot separators to reflect the module hierarchy, not file system paths. This line is invalid from the start, hence the ModuleNotFoundError.
Recap of Your Working Cases
To reinforce the rules, let’s confirm why your other attempts worked:
importlib.import_module('hello')orimportlib.import_module('hello', package='./'):'hello'is an absolute name, so thepackageargument is ignored. Python finds the top-levelhello.pyin your root directory (which is insys.path).importlib.import_module('folder.hello'): This is an absolute name directly pointing to the module inside thefolderpackage. Python resolves it by looking forfolder/hello.py(and treatsfolderas a package, even without an__init__.pyin Python 3.3+).
Fixing the Third Case
If you want to use the package parameter to import the hello module inside folder, use a relative module name with a leading dot:
# Relative import using the folder package as a base hello = importlib.import_module('.hello', package='folder') # Or just use the simpler absolute import (no package parameter needed) hello = importlib.import_module('folder.hello')
Both will load folder/hello.py and print Hello world in folder.
内容的提问来源于stack exchange,提问作者Umang Gupta




