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

I'm trying to run some tests for my python application but I'm not able to set the path correctly so that my test.py can find it.

My Application is structured like this:

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

And my test.py is looking like this:

import sys

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

Now I want to test the code within Azure Pipelines, so first I copy the repo in place:

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

After that I use tree to test if everything was copied correctly.

And then I simply run the test script by calling:

python C:/repo/tests/test.py

But that gives me a ModuleNotFoundError.

I've also tried setting the path to my main_module via PYTHONPATH but that's not working too.

Is their something I missed or is this a bug within Azure Pipelines?



Solution 1:[1]

After talking to the Azure DevOps Support, I now know that my issue is a bug within DevOps-Pipelines at the moment.

For now I'm going to copy my test-scripts next to my main module and delete them if everything was successful and before building the application.

Solution 2:[2]

To execute python script with Azure pipeline in Linux enviroment, I used 'script:'

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

Solution 3:[3]

Based on the symptoms I believe this is the same issue as: Azure pipeline can't find the user created python module

I have solved it with the following workaround

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

See my answer to the other question for full details and example YAML

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sidney Guenther
Solution 2 jvea
Solution 3 MusicalNinja