Sidney Guenther
Sidney Guenther

Reputation: 91

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?

Upvotes: 5

Views: 2777

Answers (4)

SamyIshak
SamyIshak

Reputation: 431

This worked as a bash script in my Azure Pipeline, assuming path/to/src contains your user-defined module:

echo "##vso[task.setvariable variable=PYTHONPATH;]$PYTHONPATH:$(Build.Repository.LocalPath)/path/to/src"

Note that the new PYTHONPATH is not available until the subsequent step.

Upvotes: 0

MusicalNinja
MusicalNinja

Reputation: 172

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

Upvotes: 0

jvea
jvea

Reputation: 59

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' 

Upvotes: 0

Sidney Guenther
Sidney Guenther

Reputation: 91

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.

Upvotes: 2

Related Questions