如何在azure pipelines中正确创建并激活conda环境?

问题描述 投票:0回答:1

如何在 azure 管道的 powershell 任务中使用 conda?例如以下 azure yaml:

trigger:
- master

jobs:
  - job: BuildWindows
    timeoutInMinutes: "0"
    pool:
      vmImage: 'windows-latest'
    steps:
      - powershell: Write-Host "##vso[task.prependpath]$env:CONDA\Scripts"
        displayName: Add conda to PATH
      - powershell: |
          echo "Where.exe conda: "
          where.exe conda
          echo "Where.exe pip: "
          where.exe pip
          conda shell.powershell hook
          conda create -y --name pyomexmeta python=3.8
          conda activate pyomexmeta
        displayName: test Python on windows

失败:

2020-11-21T19:18:24.7250650Z ##[section]Starting: test Python on windows
2020-11-21T19:18:24.7369794Z ==============================================================================
2020-11-21T19:18:24.7370464Z Task         : PowerShell
2020-11-21T19:18:24.7370865Z Description  : Run a PowerShell script on Linux, macOS, or Windows
2020-11-21T19:18:24.7371259Z Version      : 2.177.0
2020-11-21T19:18:24.7371605Z Author       : Microsoft Corporation
2020-11-21T19:18:24.7372055Z Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
2020-11-21T19:18:24.7372562Z ==============================================================================
2020-11-21T19:18:25.6406075Z Generating script.
2020-11-21T19:18:25.6807383Z ========================== Starting Command Output ===========================
2020-11-21T19:18:25.7048889Z ##[command]"C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\d9696f4e-d3a9-4aee-a6e6-efdce796a9b3.ps1'"
2020-11-21T19:18:25.9468041Z Where.exe conda: 
2020-11-21T19:18:26.0308376Z C:\Miniconda\Scripts\conda.exe
2020-11-21T19:18:26.6731419Z Where.exe pip: 
2020-11-21T19:18:26.6855448Z C:\Miniconda\Scripts\pip.exe
2020-11-21T19:18:26.6924643Z C:\hostedtoolcache\windows\Python\3.7.9\x64\Scripts\pip.exe
2020-11-21T19:18:33.3361701Z $Env:CONDA_EXE = "C:\Miniconda\Scripts\conda.exe"
2020-11-21T19:18:33.3363714Z $Env:_CE_M = ""
2020-11-21T19:18:33.3364965Z $Env:_CE_CONDA = ""
2020-11-21T19:18:33.3366125Z $Env:_CONDA_ROOT = "C:\Miniconda"
2020-11-21T19:18:33.3367250Z $Env:_CONDA_EXE = "C:\Miniconda\Scripts\conda.exe"
2020-11-21T19:18:33.3368113Z 
2020-11-21T19:18:33.3369110Z Import-Module "$Env:_CONDA_ROOT\shell\condabin\Conda.psm1"
2020-11-21T19:18:33.3370158Z conda activate base
2020-11-21T19:18:33.3370935Z 
2020-11-21T19:18:44.6508284Z Add-CondaEnvironmentToPromptCollecting package metadata (current_repodata.json): ...working... done
2020-11-21T19:18:44.7146158Z Solving environment: ...working... done
2020-11-21T19:18:45.8963316Z 
2020-11-21T19:18:45.8965065Z ## Package Plan ##
2020-11-21T19:18:45.8966007Z 
2020-11-21T19:18:45.8967184Z   environment location: C:\Miniconda\envs\pyomexmeta
2020-11-21T19:18:45.8968041Z 
2020-11-21T19:18:45.8968657Z   added / updated specs:
2020-11-21T19:18:45.8969167Z     - python=3.8
2020-11-21T19:18:45.8969498Z 
2020-11-21T19:18:45.8969773Z 
2020-11-21T19:18:45.8970296Z The following packages will be downloaded:
2020-11-21T19:18:45.8970730Z 
2020-11-21T19:18:45.8971222Z     package                    |            build
2020-11-21T19:18:45.8971936Z     ---------------------------|-----------------
2020-11-21T19:18:45.8972579Z     ca-certificates-2020.10.14 |                0         122 KB
2020-11-21T19:18:45.8973292Z     certifi-2020.6.20          |     pyhd3eb1b0_3         155 KB
2020-11-21T19:18:45.8973980Z     openssl-1.1.1h             |       he774522_0         4.8 MB
2020-11-21T19:18:45.8974681Z     pip-20.2.4                 |   py38haa95532_0         1.8 MB
2020-11-21T19:18:45.8975408Z     python-3.8.5               |       h5fd99cc_1        15.7 MB
2020-11-21T19:18:45.8976168Z     setuptools-50.3.1          |   py38haa95532_1         730 KB
2020-11-21T19:18:45.8976885Z     sqlite-3.33.0              |       h2a8f88b_0         809 KB
2020-11-21T19:18:45.8977602Z     vs2015_runtime-14.16.27012 |       hf0eaf9b_3         1.2 MB
2020-11-21T19:18:45.8978299Z     wheel-0.35.1               |     pyhd3eb1b0_0          38 KB
2020-11-21T19:18:45.8979018Z     ------------------------------------------------------------
2020-11-21T19:18:45.8979750Z                                            Total:        25.3 MB
2020-11-21T19:18:45.8980280Z 
2020-11-21T19:18:45.8980787Z The following NEW packages will be INSTALLED:
2020-11-21T19:18:45.8981264Z 
2020-11-21T19:18:45.8981800Z   ca-certificates    pkgs/main/win-64::ca-certificates-2020.10.14-0
2020-11-21T19:18:45.8982565Z   certifi            pkgs/main/noarch::certifi-2020.6.20-pyhd3eb1b0_3
2020-11-21T19:18:45.8985242Z   openssl            pkgs/main/win-64::openssl-1.1.1h-he774522_0
2020-11-21T19:18:45.8985950Z   pip                pkgs/main/win-64::pip-20.2.4-py38haa95532_0
2020-11-21T19:18:45.8986670Z   python             pkgs/main/win-64::python-3.8.5-h5fd99cc_1
2020-11-21T19:18:45.8987418Z   setuptools         pkgs/main/win-64::setuptools-50.3.1-py38haa95532_1
2020-11-21T19:18:45.8988138Z   sqlite             pkgs/main/win-64::sqlite-3.33.0-h2a8f88b_0
2020-11-21T19:18:45.8988862Z   vc                 pkgs/main/win-64::vc-14.1-h0510ff6_4
2020-11-21T19:18:45.8989595Z   vs2015_runtime     pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_3
2020-11-21T19:18:45.8990372Z   wheel              pkgs/main/noarch::wheel-0.35.1-pyhd3eb1b0_0
2020-11-21T19:18:45.8991098Z   wincertstore       pkgs/main/win-64::wincertstore-0.2-py38_0
2020-11-21T19:18:45.8991854Z   zlib               pkgs/main/win-64::zlib-1.2.11-h62dcd97_4
2020-11-21T19:18:45.8992345Z 
2020-11-21T19:18:45.8992645Z 
2020-11-21T19:18:45.8992935Z 
2020-11-21T19:18:45.8993392Z Downloading and Extracting Packages
2020-11-21T19:18:45.8993779Z 
2020-11-21T19:18:46.0606199Z ca-certificates-2020 | 122 KB    |            |   0% 
2020-11-21T19:18:46.3201036Z ca-certificates-2020 | 122 KB    | #3         |  13% 
2020-11-21T19:18:46.3208946Z ca-certificates-2020 | 122 KB    | ########## | 100% 
2020-11-21T19:18:46.3209635Z 
2020-11-21T19:18:46.4208293Z python-3.8.5         | 15.7 MB   |            |   0% 
2020-11-21T19:18:55.2329037Z python-3.8.5         | 15.7 MB   | #####4     |  55% 
2020-11-21T19:18:55.2337137Z python-3.8.5         | 15.7 MB   | ########## | 100% 
2020-11-21T19:18:55.2337902Z 
2020-11-21T19:18:55.3156046Z wheel-0.35.1         | 38 KB     |            |   0% 
2020-11-21T19:18:55.3160662Z wheel-0.35.1         | 38 KB     | ########## | 100% 
2020-11-21T19:18:55.3161491Z 
2020-11-21T19:18:55.7648286Z setuptools-50.3.1    | 730 KB    |            |   0% 
2020-11-21T19:18:55.7655999Z setuptools-50.3.1    | 730 KB    | ########## | 100% 
2020-11-21T19:18:55.7656876Z 
2020-11-21T19:18:56.1110966Z openssl-1.1.1h       | 4.8 MB    |            |   0% 
2020-11-21T19:18:56.1118594Z openssl-1.1.1h       | 4.8 MB    | ########## | 100% 
2020-11-21T19:18:56.1119349Z 
2020-11-21T19:18:56.2886299Z vs2015_runtime-14.16 | 1.2 MB    |            |   0% 
2020-11-21T19:18:56.2894106Z vs2015_runtime-14.16 | 1.2 MB    | ########## | 100% 
2020-11-21T19:18:56.2894923Z 
2020-11-21T19:18:56.3944261Z sqlite-3.33.0        | 809 KB    |            |   0% 
2020-11-21T19:18:56.3951479Z sqlite-3.33.0        | 809 KB    | ########## | 100% 
2020-11-21T19:18:56.3952159Z 
2020-11-21T19:18:56.4634465Z certifi-2020.6.20    | 155 KB    |            |   0% 
2020-11-21T19:18:56.4639032Z certifi-2020.6.20    | 155 KB    | ########## | 100% 
2020-11-21T19:18:56.4639817Z 
2020-11-21T19:19:01.8822350Z pip-20.2.4           | 1.8 MB    |            |   0% 
2020-11-21T19:19:01.8827563Z pip-20.2.4           | 1.8 MB    | ########## | 100% 
2020-11-21T19:19:02.2061610Z Preparing transaction: ...working... done
2020-11-21T19:19:03.6698200Z Verifying transaction: ...working... done
2020-11-21T19:19:08.2531086Z Executing transaction: ...working... done
2020-11-21T19:19:08.2687568Z 
2020-11-21T19:19:08.2688590Z #
2020-11-21T19:19:08.8382502Z 
2020-11-21T19:19:08.8383618Z ==> WARNING: A newer version of conda exists. <==
2020-11-21T19:19:08.8384315Z   current version: 4.8.3
2020-11-21T19:19:08.8384814Z   latest version: 4.9.2
2020-11-21T19:19:08.8385137Z 
2020-11-21T19:19:08.8385531Z Please update conda by running
2020-11-21T19:19:08.8385865Z 
2020-11-21T19:19:08.8386300Z     $ conda update -n base -c defaults conda
2020-11-21T19:19:08.8386667Z 
2020-11-21T19:19:08.8386927Z 
2020-11-21T19:19:08.8387204Z 
2020-11-21T19:19:08.8387731Z CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
2020-11-21T19:19:08.8388441Z If using 'conda activate' from a batch script, change your
2020-11-21T19:19:08.8390039Z invocation to 'CALL conda.bat activate'.
2020-11-21T19:19:08.8390868Z 
2020-11-21T19:19:08.8391400Z To initialize your shell, run
2020-11-21T19:19:08.8391835Z 
2020-11-21T19:19:08.8392352Z     $ conda init <SHELL_NAME>
2020-11-21T19:19:08.8392774Z 
2020-11-21T19:19:08.8393317Z Currently supported shells are:
2020-11-21T19:19:08.8393849Z   - bash
2020-11-21T19:19:08.8394890Z   - cmd.exe
2020-11-21T19:19:08.8395366Z   - fish
2020-11-21T19:19:08.8395850Z   - tcsh
2020-11-21T19:19:08.8396303Z   - xonsh
2020-11-21T19:19:08.8396781Z   - zsh
2020-11-21T19:19:08.8397271Z   - powershell
2020-11-21T19:19:08.8397672Z 
2020-11-21T19:19:08.8398177Z See 'conda init --help' for more information and options.
2020-11-21T19:19:08.8398575Z 
2020-11-21T19:19:08.8399108Z IMPORTANT: You may need to close and restart your shell after running 'conda init'.
2020-11-21T19:19:08.8399573Z 
2020-11-21T19:19:08.8399826Z 
2020-11-21T19:19:08.8400230Z # To activate this environment, use
2020-11-21T19:19:08.8400620Z #
2020-11-21T19:19:08.8401019Z #     $ conda activate pyomexmeta
2020-11-21T19:19:08.8401417Z #
2020-11-21T19:19:08.8401807Z # To deactivate an active environment, use
2020-11-21T19:19:08.8402234Z #
2020-11-21T19:19:08.8402576Z #     $ conda deactivate
2020-11-21T19:19:08.8402912Z 
2020-11-21T19:19:08.9484524Z ##[error]PowerShell exited with code '1'.
2020-11-21T19:19:08.9857513Z ##[section]Finishing: test Python on windows

我现在已经尝试了很多变体,包括使用

conda init powershell
(这很烦人,因为他已经在我 30 分钟的管道的末尾了)。

有什么想法如何在 azure 上的 powershell 任务中创建并激活 conda 环境吗?

python powershell azure-pipelines conda
1个回答
0
投票

考虑使用

conda run
命令,如下所示:

- powershell: |
    conda run -n myenv python myscript.py
  displayName: "Run myscript in myenv"

但是,如果你确实需要环境,可以尝试这个:

- powershell: |
    conda init powershell
    conda shell.powershell hook | Out-String | Invoke-Expression
    conda activate myenv
    python myscript.py

  displayName: "Run myscript in myenv
© www.soinside.com 2019 - 2024. All rights reserved.