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

Python测试脚本跨目录导入失败问题排查与解决求助

问题原因

你遇到的导入错误本质是相对路径的基准搞错了,以及Python模块导入机制的特性导致的:

  1. sys.path.append的相对路径是基于工作目录,不是脚本目录:
    当你从mother目录运行所有测试时,当前工作目录是mother/。此时test_child1.py里的sys.path.append("../")会把mother的父目录加到Python的模块搜索路径里,但test_mother.py明明在mother目录里,自然找不到。同理child2里的../../指向的是mother的爷爷目录,更不可能找到child1模块。
  2. init.py的作用是让目录成为可导入模块,但路径不对还是白搭:
    加了__init__.py只是让目录被识别为Python模块,但如果模块所在目录不在sys.path里,Python还是找不到它。
解决办法

根据你的场景,推荐几种从易到难、从规范到灵活的方案:

方案1:用pytest自动处理(最推荐,适合单元测试场景)

如果你用pytest来运行测试,根本不需要手动改sys.path,它会自动处理模块路径:

  1. 先安装pytest:
    pip install pytest
    
  2. 直接在mother目录下运行:
    pytest
    
    pytest会自动遍历所有子目录里的test_*.py文件,并且自动把当前运行目录(mother)加到sys.path,这样你在test_child1里可以直接写:
    from test_mother import Data as data
    
    在test_child2里写:
    from child1.test_child1 import Data as data
    
    完全不需要手动加sys.path,也能正常运行所有测试。

方案2:用绝对路径动态添加sys.path(兼容手动运行单个测试)

如果你不想用pytest,想手动控制路径,可以基于脚本所在的绝对路径来添加,而不是相对工作目录的路径:

  • 在test_child1.py里:
    import os
    import sys
    # 获取当前脚本所在目录的绝对路径
    script_dir = os.path.dirname(os.path.abspath(__file__))
    # 脚本在child1,所以父目录就是mother
    mother_dir = os.path.dirname(script_dir)
    sys.path.append(mother_dir)
    
    from test_mother import Data as data
    
  • 在test_child2.py里:
    import os
    import sys
    script_dir = os.path.dirname(os.path.abspath(__file__))
    # 脚本在child2,父目录就是mother
    mother_dir = os.path.dirname(script_dir)
    sys.path.append(mother_dir)
    
    from child1.test_child1 import Data as data
    
    这样不管你从哪个目录运行脚本,路径都是基于脚本实际位置计算的,不会出错。

方案3:使用Python相对导入(符合模块规范)

如果你的项目是一个标准的Python包结构(所有目录都有__init__.py),可以用相对导入:

  • 在test_child1.py里:
    from ..test_mother import Data as data
    
  • 在test_child2.py里:
    from ..child1.test_child1 import Data as data
    
    但注意:相对导入不能直接运行单个测试文件(比如python child1/test_child1.py会报错),必须从mother的上级目录,用模块方式运行:
    python -m mother.child1.test_child1
    
    或者用pytest来运行,它会自动处理相对导入的场景。

方案4:设置PYTHONPATH环境变量(一劳永逸)

你可以把mother目录的绝对路径加到系统的PYTHONPATH环境变量里,这样Python会永远把这个目录作为模块搜索路径:

  • Linux/macOS终端:
    export PYTHONPATH=/absolute/path/to/mother:$PYTHONPATH
    
  • Windows cmd:
    set PYTHONPATH=C:\absolute\path\to\mother;%PYTHONPATH%
    
  • Windows PowerShell:
    $env:PYTHONPATH = "C:\absolute\path\to\mother;$env:PYTHONPATH"
    
    设置后,你在任何目录运行测试,都可以直接写导入语句,不需要改sys.path。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:59:43