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

Python子目录单元测试无法找到manchinetranslator模块求助

Python包测试模块导入问题解决方法

问题原因

在tests子目录下直接运行测试脚本时,Python默认的模块搜索路径不包含manchinetranslator包的上层目录,导致无法找到目标模块。


解决方法

方法一:临时设置PYTHONPATH

针对不同系统,在运行测试前执行对应命令:

  • Windows命令行:
    set PYTHONPATH=C:\Python
    
    注:C:\Python是包含manchinetranslator包的上层目录(即包所在的文件夹)
  • Linux/macOS终端:
    export PYTHONPATH=/path/to/your/package-parent-directory
    

设置完成后,再运行测试脚本:

python tests/tests.py

方法二:修改测试脚本导入逻辑

在tests.py的最开头添加代码,自动将包的上层目录加入搜索路径:

import sys
import os
# 把包的上层目录加入模块搜索路径
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

import unittest
from manchinetranslator.translator import english_to_french, french_to_english

# 后续测试类代码不变
class Test_english_to_french(unittest.TestCase):
    ...   
class Test_french_to_english(unittest.TestCase):
    ...

方法三:使用pip可编辑模式安装包(推荐)

在包含manchinetranslator包的上层目录(即C:\Python)下执行:

pip install -e .

该命令会将你的包以可编辑模式安装到Python环境,此后无论在哪个路径下运行测试,都能直接导入manchinetranslator模块,且包代码修改后无需重新安装即可生效。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 18:15:38