简体   繁体   English

GitHub 由于无法识别 Python 版本而无法发布到 PyPI 的操作

[英]GitHub Actions Failing to Release to PyPI because Python Version Is Not Recognized

I'm trying to get some CI/CD experience with GitHub Actions to put on my resume so I tried to automate the publication/release process of my python package to the PyPI website.我试图通过 GitHub 操作获得一些 CI/CD 经验以放在我的简历中,因此我尝试将我的 python package 的发布/发布过程自动化到 PyPI 网站。 But so far the build fails because it doesn't detect that I have python 3.9 installed, for some reason.但到目前为止构建失败,因为它没有检测到我安装了 python 3.9,出于某种原因。

My GitHub Actions workflow looks like this:我的 GitHub Actions 工作流程如下所示:

name: Publish Python Package to PYPI

on:
  release:
    types: [published]

permissions:
  contents: read

jobs:
  deploy:

    runs-on: ubuntu-20.04

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.9.0'
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install setuptools wheel twine
    - name: Build package
      run: python setup.py sdist bdist_wheel
    - name: Publish package
      env:
        TWINE_USERNAME: __token__
        TWINE_PASSWORD: ${{ secrets.PYPI_PYPALEX_API_TOKEN }}
      run: twine upload dist/*

It's supposed to trigger upon release creation and the commands used to build the package are exactly the same as the commands I use to manually build the package: python setup.py sdist bdist_wheel它应该在发布创建时触发,用于构建 package 的命令与我用于手动构建 package 的命令完全相同: python setup.py sdist bdist_wheel

I've tried to create a release several times but the workflow fails each and every time during the Build package phase.我曾多次尝试创建一个版本,但在Build package阶段工作流程每次都失败。 If it'll help, here's the fail log:如果有帮助,这是失败日志: 在此处输入图像描述

My package has a requirement that python 3.7 or greater be installed, and python 3.9 is installed in the workflow, but it doesn't seem to recognize that.我的 package 要求安装 python 3.7 或更高版本,工作流中安装了 python 3.9,但它似乎无法识别。

I've tried googling this problem for several hours before coming here, and as far as I can tell it's either the fault of the runs-on step or the actions/checkout and actions/setup-python steps.在来到这里之前,我已经尝试用谷歌搜索这个问题几个小时,据我所知,这要么是runs-on步骤的错误,要么是actions/checkoutactions/setup-python步骤的错误。 I read last night that python 3.9 isn't available for ubuntu-latest , so I have tried several other options for the runs-on step already.我昨晚读到 python 3.9 不适用于ubuntu-latest ,所以我已经尝试了几个其他选项来runs-on步骤。 And I've tried using actions/checkout@v3 | actions/setup-python@v3我试过使用actions/checkout@v3 | actions/setup-python@v3 actions/checkout@v3 | actions/setup-python@v3 and actions/checkout@v2 | actions/setup-python@v2 actions/checkout@v3 | actions/setup-python@v3actions/checkout@v2 | actions/setup-python@v2 actions/checkout@v2 | actions/setup-python@v2 with no luck, same result. actions/checkout@v2 | actions/setup-python@v2没有运气,结果相同。 I am really new to this so I don't know if there's a certain way I can google this myself to get the answer, please go easy on me.我对此很陌生,所以我不知道是否有某种方法可以让我自己用谷歌搜索来获得答案,请 go 放轻松。

I can't tell if this is because of the version of ubuntu I'm using or if it's because of the checkout and setup-python steps.我不知道这是因为我正在使用的 ubuntu 版本,还是因为结帐和设置 python 步骤。 Does anyone who's more experienced in writing these workflows know how to solve this?在编写这些工作流程方面更有经验的人知道如何解决这个问题吗? Please?请?

Okay so after hours of searching and running through tons more of other tutorials and workflow examples I finally found what was wrong, so I'm going to close this question but leave this answer here just in case it helps someone else in the future.好吧,经过数小时的搜索和浏览大量其他教程和工作流示例后,我终于找到了问题所在,所以我将结束这个问题,但将这个答案留在这里,以防将来对其他人有所帮助。

DETAILS细节

The problem I was facing was related to the package requirements I had detailed when I made the package. Most of the tutorials I looked at had a requirements.txt file listed somewhere in the python installation steps.我面临的问题与我在制作 package 时详细说明的 package 要求有关。我查看的大多数教程在 python 安装步骤的某处列出了一个requirements.txt文件。 I didn't know what this meant until I checked through my manual build folders on my local pc and noticed that there was a file with the same name located there.我不知道这意味着什么,直到我检查了我本地电脑上的手动构建文件夹并注意到那里有一个同名文件。 After looking through my own requirements, I went back and updated the workflow to include them as well and everything now works as it should!在查看了我自己的要求之后,我返回并更新了工作流程以将它们也包含在内,现在一切正常!

SOLUTION解决方案

Package requirements need to be installed in the Install dependencies section (or wherever you install them on your own workflow)!! Package 要求需要安装在Install dependencies项部分(或在您自己的工作流程中安装它们的任何位置)!

My final workflow looks like this:我的最终工作流程如下所示:

name: Publish Python Package to PyPI and TestPyPI

on:
  push:
    branches: [ main ]

jobs:
  deploy:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Set up Python 3.9
      uses: actions/setup-python@v3
      with:
        python-version: '3.9'
    - name: Install Dependencies
      run: |
        python -m pip install --upgrade pip
        python -m pip install Pillow numpy filetype setuptools wheel twine
    - name: Build Source and Wheel Distributions
      run: |
        python setup.py sdist bdist_wheel
    - name: Publish Distribution to Test PyPI
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        password: ${{ secrets.TEST_PYPI_PYPALEX_API_TOKEN }}
        repository_url: https://test.pypi.org/legacy/
        skip_existing: true
    - name: Publish Distribution to PyPI
      if: startsWith(github.ref, 'refs/tags')
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        password: ${{ secrets.PYPI_PYPALEX_API_TOKEN }}

As a side note:作为旁注:
Using python -m build did not work for me so I had to use python setup.py sdist bdist_wheel in order to build my package (as I realize some tutorial out there say to use python build ).使用python -m build对我不起作用,所以我不得不使用python setup.py sdist bdist_wheel来构建我的 package(因为我意识到那里的一些教程说使用python build )。

The tutorials I followed to initially set up the workflow were:我最初设置工作流程所遵循的教程是:

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

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