首页 > 编程问答 >使用 Azure DevOps 管道时找不到模块“twine”

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

时间:2024-07-23 13:35:25浏览次数:12  
标签:python windows azure-devops pip azure-pipelines

我正在 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'

这个问题的原因是,即使在虚拟环境中安装了 twine ,也尝试在虚拟环境外运行它。

以下是解决此问题的步骤以及对的代码的一些解释:

已修正的代码:

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

- script: |
    python -m pip install --upgrade pip setuptools wheel twine
    python -m venv myenv
    myenv/Scripts/activate
    pip install -r requirements.txt # 如果有 requirements.txt,则在此处安装依赖项
  displayName: 'Install Python Prerequesites'

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

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

- script: |
    python -m twine upload --repository-url https://pkgs.dev.azure.com/myorg/myproject/_packaging/myfeed/pypi/upload/ dist/*.whl
  displayName: 'Upload to feed'
  workingDirectory: $(System.DefaultWorkingDirectory) # 可选:确保位于正确的目录中

说明:

  1. 虚拟环境激活: 确保在安装 twine 之后 立即激活虚拟环境 ( myenv/Scripts/activate )。这可确保 twine 安装在虚拟环境中,并且可以在后续步骤中访问。
  2. 在虚拟环境中运行 twine 不需要直接调用 twine 。请在 Upload to feed 步骤中使用 python -m twine ... 。这将使用虚拟环境中的 Python 解释器来运行 twine 模块。
  3. requirements.txt(可选): 如果的项目有 requirements.txt 文件,请在安装 twine 后,在虚拟环境激活的步骤中添加 pip install -r requirements.txt 。这将确保所有必要的依赖项都安装在虚拟环境中。
  4. 工作目录(可选): workingDirectory 参数(在 Upload to feed 步骤中)是可选的。如果不确定脚本在哪个目录中运行,可以使用它来指定工作目录。

其他提示:

  • 确保的 Azure DevOps 代理具有构建 Python 包所需的工具和依赖项。
  • 可以使用 print(sys.executable) print(sys.path) 来检查的脚本正在使用哪个 Python 解释器和哪些搜索路径。
  • 如果仍然遇到问题,请尝试在每个步骤中添加 echo $PATH 以查看环境变量,并确保虚拟环境的路径包含在其中。

通过进行这些更改,应该能够在 Azure DevOps 管道中成功使用 twine 将的 Python 包上传到 Azure Artifacts。

标签:python,windows,azure-devops,pip,azure-pipelines
From: 78780083

相关文章

  • python-input键盘输入
     str=input("请输入:")#用户键盘输入#str表示一个字符串类型的变量,input会将读取到的字符串放入str中print(str) aa='请输入:'str=input(aa)#用户键盘输入#str表示一个字符串类型的变量,input会将读取到的字符串放入str中print(str)      ......
  • Python从0到100(四十五):从字符串到前后端分离
    前言:零基础学Python:Python从0到100最新最全教程。想做这件事情很久了,这次我更新了自己所写过的所有博客,汇集成了Python从0到100,共一百节课,帮助大家一个月时间里从零基础到学习Python基础语法、Python爬虫、Web开发、计算机视觉、机器学习、神经网络以及人工智能相关知......
  • Python的运算符与条件表达式
    一、运算符Python数据是通过使用运算符来进行操作的,与数学运算符类似,主要运用于数字计算,比较大小和逻辑运算。Python中的运算符主要包括算术运算符、赋值运算符、比较运算符、逻辑运算符和位运算符。1.算术运算符算术运算符用在数字表达式中,作用和在数学中是一样的。Python......
  • 在尝试使用 docker 运行基于 pythonnet 的项目时,找不到 C# 运行时
    屏幕截图我不断收到此错误:RuntimeError:Failedtocreateadefault.NETruntime,whichwouldhavebeen"mono"onthissystem.Eitherinstallacompatibleruntimeorconfigureitexplicitlyvia......
  • Python 在模块中使用变量
    如何将一个文件中声明的变量调用到另一个文件中?大家好我已经在file1中声明了我的变量。我的函数位于file2中,它使用该变量。如何调用主文件中声明的变量。提前致谢当然可以!以下是如何在Python中跨文件使用变量:file1.py(包含变量的文件)#在file1.p......
  • Python 类型提示和 linter
    我一直在向我们的python项目添加静态类型检查,例如像这样:fromtypingimportListfromsomethingimportMyOtherClassclassMyClass:def__init__(self)->None:self.some_var=None#type:List[MyOtherClass]但是,现在我们使用的linter(flake8和......
  • eclipse如何写python程序
    本文主要介绍在Windows系统环境下,搭建能在Eclipse中运行python程序的环境。一、Eclipse下载与安装:Eclipse是写JAVA的IDE,下载地址为:http://www.eclipse.org/downloads/下载安装,网上教程很多,不赘述。二、pydev插件下载与安装:启动Eclipse,点击Help—>EclipseMarketplace......
  • 运行 python 3 代码时出现 python 2 语法错误
    我有一个如下所示的类classExperimentResult(BaseDataObject):def__init__(self,result_type:str,data:dict,references:list):super().__init__()self.type=result_typeself.references=referencesself.data=data......
  • 如何让 python 类型检查器知道它应该返回其类的新实例?
    我想使用classmethod返回当前类的新实例,并且我尝试了如下代码,但它引发了NameError('name'T'isnotDefined')PutthecodeT=TypeVar('T',bound=A)on|||以上也不起作用。classA有什么好主意来处理它吗?Isthereanygoodideatohandleit?......
  • 由于循环依赖而导致的Python注释错误
    我有两个相互依赖的类,并且无需注释即可正常工作。不幸的是,当我尝试注释返回值时,它会导致预期循环依赖错误。Network.pydefprocessors(self)->List[Processor]:#implementationProcessor.pydefnetwork(self)->Network:......