Home > Software engineering >  GitHub Actions Failing to Release to PyPI because Python Version Is Not Recognized
GitHub Actions Failing to Release to PyPI because Python Version Is Not Recognized

Time:02-02

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. But so far the build fails because it doesn't detect that I have python 3.9 installed, for some reason.

My GitHub Actions workflow looks like this:

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

I've tried to create a release several times but the workflow fails each and every time during the Build package phase. If it'll help, here's the fail log: enter image description here

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.

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. 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. And I've tried using actions/checkout@v3 | actions/setup-python@v3 and actions/checkout@v2 | actions/setup-python@v2 with no luck, same result. 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.

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. Does anyone who's more experienced in writing these workflows know how to solve this? Please?

CodePudding user response:

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. 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)!!

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).

The tutorials I followed to initially set up the workflow were:

  • Related