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

Python导入模块中import语句工作机制及多场景正确导入方法

Hey there! This is such a common stumbling block with Python's import system—let me break down exactly what's happening here, then show you the fix.

Python's Import Mechanism: The Core of Your Problem

To understand why you're seeing these errors, we need to cover two key parts of how Python finds and loads modules:

1. The Module Search Path (sys.path)

When Python runs a script, it looks for modules in the directories listed in sys.path. Here's the critical detail:

  • If you run a script directly (like python a.py or python module/b.py), the directory containing that script gets added to the start of sys.path automatically.
    • When you run a.py from the project/ folder: sys.path includes project/, so Python can see the module package and everything inside it.
    • When you run b.py directly from the module/ folder: sys.path only includes module/, not project/—so Python has no idea the module package even exists.

2. Absolute vs. Relative Imports

Python has two main ways to import modules, and they behave differently depending on context:

  • Absolute imports: Use the full path from a top-level package (e.g., from module import c). These work only if the parent directory of that top-level package is in sys.path.
  • Relative imports: Use dots to reference sibling or parent modules (e.g., from . import c). These only work when the script is being run as part of a package (not as a standalone top-level script).

Why Your Original Code Failed

  • When you used import c in b.py and ran a.py: Python looked for c.py in sys.path (which was project/), not in module/—hence the ModuleNotFoundError.
  • When you switched to from module import c in b.py: a.py worked because project/ is in sys.path, so Python could find the module package. But running b.py directly failed because sys.path was module/, so Python couldn't locate the module package itself.
The Correct Import Approach for Your Scenario

You've got two solid options, depending on how you need to run b.py:

Option 1: Use Relative Imports + Run as a Package Module

This is the cleanest, most "Pythonic" approach for packages that are meant to be imported and occasionally run standalone.

In b.py, replace your import with a relative one:

# b.py
from . import c

# Rest of your code goes here
  • This works perfectly when b.py is imported by a.py (since it's part of the module package).
  • To run b.py standalone, don't run it directly. Instead, navigate to the project/ directory and run:
    python -m module.b
    
    The -m flag tells Python to treat module.b as a package module, which adds project/ to sys.path and lets the relative import work correctly.

Option 2: Add the Parent Directory to sys.path (For Direct Script Runs)

If you absolutely need to run b.py directly (without the -m flag), you can modify sys.path at the top of b.py to include the parent project/ directory:

# b.py
import sys
from pathlib import Path

# Add the parent directory (project/) to the module search path
sys.path.append(str(Path(__file__).parent.parent))

from module import c

# Rest of your code goes here
  • This makes the absolute import work both when b.py is imported by a.py and when run directly.
  • Note: This is a bit of a workaround, but it's useful if direct script execution is a hard requirement.

Bonus Best Practice

For projects that need both importable packages and standalone scripts, a common pattern is to create a top-level scripts/ folder (next to module/) that imports from your package. This keeps your module files clean and avoids messing with sys.path inside them.

内容的提问来源于stack exchange,提问作者mannerpots

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:33:09