python - 通过命令提示符运行 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
我的目录放置有什么问题吗?如果不是,那么是什么导致了这个错误。
解决方案
在 pytest.ini 配置文件中添加实用程序路径,如下所示:-
[pytest]
python_paths = . <path of utility>
推荐阅读
- python - 不支持 Python Zipfile 压缩方法
- discord - Error when outputting an image inside an embed with canvas | Discord.js
- python - 如何解决 Python 中类方法的 NameError?
- spring-boot - SpringBoot OAuth2 error "Full authentication is required to access this resource"
- pytest - How does Pytest figure out that the monkeypatch fixture is for monkeypatching?
- c# - Discord.Net 使用 Bots 头像作为 IconURL/AvatarURL
- symfony - 如何通过在 Symfony 5 中注销将用户电子邮件放入 Logger 文件
- angular - How to pass observable from params to pipe rxjs
- arrays - Check if streets are the same or not in array
- python - PyQt 突然停止显示 QMessage?