简体   繁体   English

Azure DevOps 找不到 python 模块

[英]Azure DevOps can't find python module

I am attempting to run tests from a python module using Azure DevOps.我正在尝试使用 Azure DevOps 从 python 模块运行测试。 I've got a pipeline build set up to build off a yml file.我已经建立了一个管道构建来构建一个 yml 文件。 I'm getting an error that my module name on my imports is not right, but it doesn't look like that the module name is getting set in the build?我收到一个错误,我的导入中的模块名称不正确,但看起来模块名称没有在构建中设置?

When I run this locally, it works just fine.当我在本地运行它时,它工作得很好。

Module repo file structure:模块 repo 文件结构:

api_check
api_check/auth
api_check/etc
api_check/__init__.py

I'm running the tests using the following in my yml file: - script: |我在我的 yml 文件中使用以下内容运行测试: - 脚本: | mkdir logs pytest -m smoke --junitxml=logs/test-results.xml --ENV=prod displayName: 'Test with pytest' mkdir 日志 pytest -m 烟雾 --junitxml=logs/test-results.xml --ENV=prod displayName: '用 pytest 测试'

This step is giving the following error:这一步出现以下错误:

2018-10-11T21:51:02.9825682Z _______________________ ERROR collecting test_search.py ________________________
2018-10-11T21:51:02.9826322Z ImportError while importing test module '/home/vsts/work/1/s/test_search.py'.
2018-10-11T21:51:02.9826804Z Hint: make sure your test modules/packages have valid Python names.
2018-10-11T21:51:02.9827123Z Traceback:
2018-10-11T21:51:02.9827411Z test_search.py:12: in <module>
2018-10-11T21:51:02.9827745Z     from api_check.auth import get_Token_UsernamePassword
2018-10-11T21:51:02.9828644Z E   ModuleNotFoundError: No module named 'api_check'

Is there something I need to add to get the build to recognize the module api_check?是否需要添加一些内容才能使构建识别模块 api_check? Is there some sort of build variable I need to read in?我需要读入某种构建变量吗?

Edit for more information-编辑以获取更多信息-

I am running this from whatever the directory is that the yml file gets run from my default - based off the debug log, the working directory is /home/vsts/work/1/s .我正在从任何目录运行它,yml 文件从我的默认运行 - 基于调试日志,工作目录是/home/vsts/work/1/s When I list the contents of the working directory, I see all the contents of my module, everything that in my repo is under api_check .当我列出工作目录的内容时,我会看到我的模块的所有内容,我的 repo 中的所有内容都在api_check下。

So, rather than what I would expect from pulling down a repo:所以,而不是我对拉下回购的期望:

$workingDirectory/api_check
$workingDirectory/api_check/auth
$workingDirectory/api_check/etc
$workingDirectory/api_check/__init__.py

it is instead:而是:

$workingDirectory
$workingDirectory/auth
$workingDirectory/etc
$workingDirectory/__init__.py

So it looks like just adding the parent directory api_check wouldn't help, as there appears to be no api_check directory.所以看起来只是添加父目录api_check无济于事,因为似乎没有api_check目录。 Is there some setting I can flip in the pipeline sources to have it include the top-level folder api_check ?是否有一些设置我可以在管道源中翻转以使其包含顶级文件夹api_check

Turns out, Azure expands the repo so that the highest level directory in the repo becomes the working directory, rather than being in the working directory.事实证明,Azure 扩展了 repo,以便 repo 中的最高级别目录成为工作目录,而不是工作目录中。

To solve my problem, I moved my module to a second-level directory in my repo.为了解决我的问题,我将我的模块移到了我的 repo 中的二级目录。

ex, GitHub repo layout:例如,GitHub 存储库布局:

$repo/api_check
$repo/api_check/auth
$repo/api_check/etc
$repo/api_check/__init__.py

becomes变成

$workingDirectory/api_check
$workingDirectory/api_check/auth
$workingDirectory/api_check/etc
$workingDirectory/api_check/__init__.py

There may be a way to make Azure expand the repo as a sub-directory in the working dir, but I haven't found it.可能有一种方法可以让 Azure 将 repo 扩展为工作目录中的子目录,但我还没有找到。

可以尝试删除根目录下的__init__.py ,应该可以解决问题。

您只需要使用以下命令执行测试:(使用 python -m)

python -m pytest -v test.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM