pentti
pentti

Reputation: 105

python can't find '__main__' module in '.py' in Azure Pipelines

My goal is to deploy and run my python script from GitHub to my virtual machine via Azure Pipeline. My azure-pipelines.yml looks like this:

jobs: 
- deployment: VMDeploy
  displayName: Test_script
  environment:
    name: deploymentenvironment
    resourceType: VirtualMachine
  strategy:
      rolling:
        maxParallel: 2  #for percentages, mention as x%
        preDeploy:
          steps:
          - download: current
          - script: echo initialize, cleanup, backup, install certs
        deploy:
          steps:
          - task: Bash@3
            inputs:
              targetType: 'inline'
              script: python3 $(Agent.BuildDirectory)/test_file.py
        routeTraffic:
          steps:
          - script: echo routing traffic
        postRouteTraffic:
          steps:
          - script: echo health check post-route traffic
        on:
          failure:
            steps:
            - script: echo Restore from backup! This is on failure
          success:
            steps:
            - script: echo Notify! This is on success

This returns an error:

/usr/bin/python3: can't find '__main__' module in '/home/ubuntu/azagent/_work/1/test_file.py'

##[error]Bash exited with code '1'.

If I place the test_file.py to the /home/ubuntu and replace the deployment script with the following: script: python3 /home/ubuntu/test_file.py the script does run smoothly.

If I move the test_file.py to another directory with mv /home/ubuntu/azagent/_work/1/test_file.py /home/ubuntu I can find an empty folder, not a .py file, named of test_file.py

EDIT

Screenshot from Jobs:

enter image description here

Upvotes: 1

Views: 339

Answers (1)

Cece Dong - MSFT
Cece Dong - MSFT

Reputation: 31063

The reason that you can not get the source is because you use download: current to download artifacts produced by the current pipeline run, but you didn't publish any artifact in current pipeline.

As deployment jobs doesn't automatically check out source code, you need either checkout the source in your deployment job,

       - checkout: self

or publish the sources to the artifact before downloading it.

      - publish: $(Build.SourcesDirectory)
        artifact: Artifact_Deploy

Upvotes: 1

Related Questions