简体   繁体   English

python 在 Azure 管道的“.py”中找不到“__main__”模块

[英]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.我的目标是通过 Azure 管道将我的 python 脚本从 GitHub 部署并运行到我的虚拟机。 My azure-pipelines.yml looks like this:我的azure-pipelines.yml看起来像这样:

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.如果我将test_file.py放到/home/ubuntu并用以下内容替换部署脚本: script: python3 /home/ubuntu/test_file.py脚本运行顺利。

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如果我使用mv /home/ubuntu/azagent/_work/1/test_file.py /home/ubuntutest_file.py移动到另一个目录,我可以找到一个名为test_file.py的空文件夹,而不是.py文件

EDIT编辑

Screenshot from Jobs:乔布斯截图:

在此处输入图像描述

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.无法获取源的原因是因为您使用download: current下载当前管道运行产生的工件,但您没有在当前管道中发布任何工件。

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

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

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