PyPI Setup

Optionally, your package can automatically be released on PyPI whenever you push a new tag to the master branch.

Configuring PyPi Credentials

To publish to PyPI, you can set your credentials for the repository named pypi.

Note that it is recommended to use API tokens when uploading packages to PyPI. Once you have created a new token, you can tell Poetry to use it:

$ poetry config pypi-token.pypi my-token

If you still want to use your username and password, you can do so with the following call to config.

$ poetry config http-basic.pypi username password

Packaging

Before you can publish your library, you will need to package it.

$ poetry build

This command will package your library in two different formats: sdist which is the source format, and wheel which is a compiled package.

Once that’s done you are ready to publish your library

Publishing to PyPI

Alright, so now you can publish packages.

Poetry will publish to PyPI by default. Anything that is published to PyPI is available automatically through Poetry.

If we wanted to share a package with the Python community, we would publish on PyPI as well. Doing so is easy.

$ poetry publish

This will package and publish the library to PyPI, at the condition that you are a registered user and you have configured your credentials properly.

The publish command does not execute build by default.

If you want to build and publish your packages together, just pass the –build option.

$ poetry publish --build

Once this is done, your library will be available to anyone.

GitHub Actions Setup

Github Actions can automatically release your Python package to PyPI after a successful build.

For a minimal configuration, generate a PyPI API token and add it to the Secrets tab in your project settings in GitHub.

name: Release

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 and set up Poetry
              run: |
                  python -m pip install --upgrade pip
                  pip install --upgrade poetry --pre
                  poetry config virtualenvs.in-project true

            - name: Install dependencies
              run: poetry install

            - name: Build and publish
              env:
                  PYPI_TOKEN: ${{ secrets.PYPI_TOKEN }}
              run: |
                  poetry config pypi-token.pypi $PYPI_TOKEN
                  poetry publish --build

It is also possible, but not recommended, to use PyPI user and password, instead of a token.

Deploying tags

Most likely, you would only want to deploy to PyPI when a new version of your package is cut. To do this, you can tell GitHub Actions to only deploy on tagged commits, like so:

on:
    release:
        types: [created]

If you tag a commit locally, remember to run git push --tags to ensure that your tags are uploaded to GitHub.

Your Release Process

If you are using this feature, this is how you would do a patch release:

$ poetry version patch
$ git tag `poetry version -s`
$ git push --tags

This will result in:

  • mypackage 0.1.1 showing up in your GitHub tags/releases page

  • mypackage 0.1.1 getting released on PyPI

You can also replace the patch with minor or major.