首页 > 解决方案 > 通过命令提示符运行 python 程序时未找到模块错误

问题描述

我正在通过 git bash shell 上的 runtests.sh 文件运行单元测试$ sh runtests.sh。这是我的目录结构的样子。

|- project
|   |-- transformations
|       |-- udf_functions
|           |-- udf_serviceA
|               |-- function1.py
|               |-- function2.py
|- unittests
|   |-- tests_for_serviceA
|        |-- unit_tests.py
|- requirements.txt
|- install_requirements.cmd
|- runtests.sh

我的runtests.sh内容是:

python unittests/tests_for_serviceA/unit_tests.py
py.test --cov=. --cov-branch --cov-report=html --junitxml=test_results.xml test "$@"
coverage xml

当我在 GitBash 命令行上运行此文件时,它会导入 python 中的所有其他内置模块,但不会导入我编写的模块。例如,它会抛出一个错误,即module function1 not found. 在脚本中,我将函数导入为from transformations.udf_functions.udf_serviceA import function1

我的目录放置有什么问题吗?如果不是,那么是什么导致了这个错误。

标签: pythongit-bash

解决方案


在 pytest.ini 配置文件中添加实用程序路径,如下所示:-

[pytest]
python_paths = . <path of utility>

推荐阅读