首页 > 解决方案 > 无法在我的单元测试文件夹中导入 python 函数

问题描述

我的项目结构如下:

在此处输入图像描述

我现在想在测试文件夹中添加一些 UT,特别是在 UT_data_handler.py 中。使用这个 UT 文件,我正在尝试导入我想要测试的函数,例如:

from binary.data_handler import extract_data_file, convert_all_data_to_dict

之后我收到以下错误:

> python UT_data_handler.py
Traceback (most recent call last):
  File "UT_data_handler.py", line 3, in <module>
    from binary.data_handler import extract_data_file, convert_all_data_to_dict
ImportError: No module named binary.data_handler

有人可以提供建议/解决方案吗?

标签: pythonunit-testingpython-unittestpython-3.8

解决方案


(一处)Python 在当前工作目录中查找导入的文件。所以尝试从那里执行文件:

$ python test/UT_data_handler.py

推荐阅读