使用 Azure DevOps 管道时找不到模块“twine”

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

我正在 Azure Devops 中运行一个管道,该管道应该使用“twine”发布一个轮子。该管道使用基于 Windows 的映像。

管道基于 https://learn.microsoft.com/en-us/azure/devops/pipelines/artifacts/pypi?view=azure-devops&tabs=yaml#publish-python-packages-to- 的文档an-azure-artifacts-feed.

我尝试过使用和不使用虚拟环境并直接调用 twine,但也使用

python -m
。还使用
python3
而不是
python
进行了测试,但没有运气。

我总是会遇到错误

C:\__t\Python\3.9.13\x64\python.exe: No module named twine 

步骤如下:

- task: UsePythonVersion@0
        inputs:
          versionSpec: '3.x'
          disableDownloadFromRegistry: true
          addToPath: true
          architecture: 'x64'
        displayName: 'Use Python $(python.version)'

      - script: |
          python -m pip install --user --upgrade pip setuptools wheel twine
          python -m venv myenv
          myenv/Scripts/activate
        displayName: 'Install Python Prerequesites'

      - script: |
          python setup.py bdist_wheel
        displayName: 'Build wheel'

      - task: TwineAuthenticate@1
        inputs:
          artifactFeed: 'MyProject/MyFeed'
        displayName: 'Twine Authenticate'

      - script: |
          # Also tried python -m twine...
          twine upload --repository-url https://pkgs.dev.azure.com/myorg/myproject/_packaging/myfeed/pypi/upload/ dist/*.whl
        displayName: 'Upload to feed'
python windows azure-devops pip azure-pipelines
1个回答
0
投票

根据此 document,我们应该引用

$(PYPIRC_PATH)
任务生成的变量
TwineAuthenticate@1
将 twine 包发布到 Azure Artifacts feed。

这是在

windows-latest
Microsoft 托管 代理上运行的示例 YAML 管道。

pool:
  vmImage: windows-latest

stages:
- stage: CI
  jobs:
  - job: Build
    strategy:
      matrix:
        Python309:
          python.version: '3.9'
        Python312:
          python.version: '3.12'
    steps:
    - task: replacetokens@6
      inputs:
        root: '$(System.DefaultWorkingDirectory)'
        sources: 'setup.py'
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '$(python.version)'
      displayName: 'Use Python $(python.version)'

    - script: |
        python -m pip install --user --upgrade pip setuptools wheel twine
        python -m venv myenv
        myenv/Scripts/activate
      displayName: 'Install Python Prerequesites'

    - script: |
        python setup.py bdist_wheel
      displayName: 'Build wheel'

    - task: TwineAuthenticate@1
      inputs:
        artifactFeed: '$(ProjectName)/ProjectScopeFeed-twine'
      displayName: 'Twine Authenticate'

    - script: |
        python --version
        twine --version
        twine upload -r ProjectScopeFeed-twine --config-file $(PYPIRC_PATH) dist/*.whl
      displayName: 'Upload to feed'
    
    - publish: $(PYPIRC_PATH)
      artifact: pypirc$(python.version)

Image

如果问题仅影响在自托管代理上运行的管道,您将需要仔细检查是否已正确配置自托管代理以使用此任务UsePythonVersion@0,因为,

自托管代理不支持下载 python 版本。您只能使用预装版本。

此外,变量

$(PYPIRC_PATH)

指向包含管道服务帐户令牌的
.pypirc
文件。因此,还请让各自(根据您的
作业授权范围)或两个管道服务帐户拥有足够的权限将包发布到 feed。

Image

© www.soinside.com 2019 - 2024. All rights reserved.