共用方式為


PythonScript@0 - Python 腳本 v0 工作

使用此工作來執行 Python 檔案或內嵌腳本。

語法

# Python script v0
# Run a Python file or inline script.
- task: PythonScript@0
  inputs:
    scriptSource: 'filePath' # 'filePath' | 'inline'. Required. Script source. Default: filePath.
    scriptPath: # string. Required when scriptSource = filePath. Script path. 
    #script: # string. Required when scriptSource = inline. Script. 
    #arguments: # string. Arguments. 
  # Advanced
    #pythonInterpreter: # string. Python interpreter. 
    #workingDirectory: # string. Working directory. 
    #failOnStderr: false # boolean. Fail on standard error. Default: false.

輸入

scriptSource - 文稿來源
string。 必須的。 允許的值:filePath (檔案路徑),inline。 預設值:filePath

指定腳本是來源樹狀結構中的檔案,還是內嵌在此工作中寫入。


scriptPath - 文稿路徑
stringscriptSource = filePath時為必要項。

指定要執行的文稿路徑。 必須是完整路徑或相對於 $(System.DefaultWorkingDirectory)


script - 文稿
stringscriptSource = inline時為必要項。

指定要執行的 Python 腳本。


arguments - 自變數
string

指定透過 sys.argv傳遞至腳本執行的自變數,就像您在命令行上傳遞一樣。


pythonInterpreter - Python 解釋器
string

指定要使用的 Python 解釋器絕對路徑。 如果未指定,工作將會使用PATH中的解釋器。
執行 使用 Python 版本 工作,將 Python 版本新增至 PATH。


workingDirectory - 工作目錄
string

指定文稿執行所在的工作目錄。 如果未指定,則會使用 System.DefaultWorkingDirectory 的值。 針對組建,此變數預設為存放庫的根目錄。 針對版本,它會預設為成品目錄的根目錄。


failOnStderr - 標準錯誤 失敗
boolean。 預設值:false

如果設定為 true,如果任何文字寫入 stderr 數據流,此工作將會失敗。


工作控制選項

除了工作輸入之外,所有工作都有控制選項。 如需詳細資訊,請參閱 控制項選項和一般工作屬性

輸出變數

沒有。

備註

根據預設,此工作會從系統路徑叫用 python。 執行 使用 Python 版本 將您想要的版本放入系統路徑中。

範例

執行內嵌 Python 腳本。

- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      print('Hello world 1')
      print('Hello world 2')

執行使用環境變數的內嵌 Python 腳本。

- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      import os
      print(f'Environment variable MY_VAR: {os.getenv("MY_VAR")}')
  env:
    MY_VAR: 'Hello, World!'

從工作目錄執行 Python 腳本。 如果任何文字寫入至 stderr 數據流,工作將會失敗。

- task: PythonScript@0
  inputs:
    scriptSource: 'filePath'
    scriptPath: 'scripts/hello_world.py'
    workingDirectory: '$(Build.SourcesDirectory)/scripts'
    failOnStderr: true

需求

要求 說明
管線類型 YAML、傳統組建、傳統版本
執行於 Agent、DeploymentGroup
需求 沒有
功能 此工作不符合作業中後續工作的任何需求。
命令限制 任意
Settable 變數 任意
代理程式版本 所有支援的代理程式版本。
工作類別 效用