Github workflow can be used for building and Testing and Uploading Packages to Python Package Index.
PYPI better known for the use of pip command in Python environment, is a repository of popular opensource projects. You can too contribute your packages to the community. All you need is an account with few setup tools.
Prepare You Python Package
First up all you have to prepare your Python package . Make sure package consist of
- Setup.py
- Readme.md file
Setup.py
Here is a sample code of setup.py
import os
with open("readme.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="PACAKGE-NAME-USERNAME", # Replace with your own username
version="1.0.1",
author="author name",
author_email="email",
description="short description",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/udername/pakg-repo",
packages=setuptools.find_packages(),
install_requires=[
'pacakge1','package2'
],
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
Readme.md
Read me file consist of
- Repository/package details
- Requirements
- How to use the repo etc
Use a markdown page for serving the readme file
Github Repository and Action
Now it is time for setup workflow for the python repository you have uploaded to the Github.com
Get PYPI account
As prerequisite for this tutorial you need to register an account in PYPi.org.
Repository secret
Go to you repository settings-Secrets and add the following secrets
PYPI_USERNAME
PYPI_PASSWORD
GitHub Secret
Add a workflow for upload a Python Package
Add a action , use manual method and copy following Upload PYPI workflow file also name file as UploadPYPI.yml
# This workflows will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
name: Upload PYPI
.
release:
types: [created]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
The above workflow will install dependencies on a Ubuntu cloud machine, then build the package, then upload the package to testpypi repository using twine and secret you have added to your repository, namely PYPI_USERNAME and PYPI_PASSWORD.
As we specified on the on section of the workflow, will execute the action whenever you create a new release.
To test the workflow create a release with a tag starting with v*, eg:- v1,v1.2.1 etc and the workflow will fire the actions.
You can check for result of the workflow at the actions link of your repository, also on the top of repo a notification will shown if the workflow is executed.
Tetsing The installed repo
Now check your Pypi account, under the account you can see, the uploaded projects. If the project was found on the list, can install them with pip
pip install <YOUR-PACKAGENAME>
Error
You may have caught some error while retrying publishing packages. One of them was name can’t be reused.
Pypi/TestPYPI not allowing you to reuse the package name, even if you have deleted your package from the Project list.
So Name your package with care. To change the package Name use the setup.py