Azure DevOps 管道 - Python - ModuleNotFoundError 尽管 sys.path.append() 或设置 PYTHONPATH

Azure DevOps Pipelines - Python - ModuleNotFoundError despite sys.path.append() or setting PYTHONPATH

我正在尝试 运行 对我的 python 应用程序进行一些测试,但我无法正确设置路径以便我的 test.py 可以找到它。

我的申请结构如下:

repo/src/main/python/main_module
repo/tests/test.py

我的 test.py 看起来像这样:

import sys

sys.path.append(os.path.normpath('C:/repo/src/main/python'))
import main_module

现在我想在 Azure Pipelines 中测试代码,所以首先我将存储库复制到位:

- powershell: |
    cd C:/
    mkdir repo
    cp -r -v $(src.repo)/* C:/repo/
  condition: eq( variables['Agent.OS'], 'Windows_NT' )

之后我使用 tree 来测试是否所有内容都被正确复制。

然后我通过调用 运行 简单地 运行 测试脚本:

python C:/repo/tests/test.py

但这给了我一个 ModuleNotFoundError

我也试过通过 PYTHONPATH 设置我的 main_module 的路径,但这也不起作用。

他们是我错过的东西还是 Azure Pipelines 中的错误?

在与 Azure DevOps 支持部门交谈后,我现在知道我的问题是目前 DevOps-Pipelines 中的一个错误。

现在,我将把我的测试脚本复制到我的主模块旁边,如果一切顺利,在构建应用程序之前删除它们。

要在 Linux 环境中使用 Azure 管道执行 python 脚本,我使用了 'script:'

script: pythonpath=/repo/src/main/python python3 repo/tests/test.py 
displayName: 'execute script python' 

根据症状,我认为这与以下问题相同: Azure pipeline can't find the user created python module

我已经通过以下解决方法解决了这个问题

include a script in the pipeline to move the source code into a new suitably named directory and then test it there.

查看我对其他问题的回答以获取完整的详细信息和 YAML 示例