You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Python3中使用importlib动态导入模块报错问题咨询

Understanding 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 in sys.path (which includes your script’s root directory by default when you run main.py directly).
  • 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 the package parameter 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') or importlib.import_module('hello', package='./'): 'hello' is an absolute name, so the package argument is ignored. Python finds the top-level hello.py in your root directory (which is in sys.path).
  • importlib.import_module('folder.hello'): This is an absolute name directly pointing to the module inside the folder package. Python resolves it by looking for folder/hello.py (and treats folder as a package, even without an __init__.py in 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

火山引擎 最新活动