Pushing PyPI Packages to Gitea

Last modified by DevOps-as-a-Service Operator on 2025/02/05 11:33

This step-by-step guide outlines how to push your Python packages to a Gitea registry using Twine. 

Requirements

  • To work with the PyPI package registry, you need to use the package pip to consume and twine to publish packages.
  • To push packages, a user with read-write permissions is required. This user can either be a technical user or a human user.
  • For PyPI packages, authentication can be achieved using either a password or a token. However, for npm packages, pushing to the registry is only possible with a token.

Generating a Token

To push packages to Gitea, you must have an access token with read and write permissions. The Creating Access Tokens for Pushing Packages to Gitea documentation guides you on how to create such a token.


Guide

Configuration

For configuring .pypirc in Windows or Linux:

  1. Open/Create .pypirc file: 
    In Windows, navigate to C:\Users\YourUsername or in Linux to ~, and create or open the .pypirc file.
     
  2. Edit .pypirc Content:
    Add the [distutils] section to declare gitea as an index server.
    In the [gitea] section specify your Gitea repository URL, replacing the placeholders {pkey}, {username} and {password} with actual values.
    [distutils]
    index-servers = gitea

    [gitea]
    repository = https://gitea-CUSTOMER.devops.t-systems.net/api/packages/{pkey}/pypi
    username = {username}
    password = {password}
    PlaceholderDescription
    pkeyProject Key in DevOps Portal, refers to Organization in Gitea
    usernameYour Gitea username or the name of the technical user
    password

    Your password or access token.

    A token has to be used if 2FA is configured for the user.
    Refer to Creating Access Tokens for Pushing Packages to Gitea for instructions.

    Warning

    A package always belongs to an organization, not a repository. To link an (already uploaded) package to a repository, open the settings page on that package and choose a repository to link this package to. The entire package will be linked, not just a single version.

  3. Save .pypirc:
    Ensure no file extension like .txt when saving. In Linux, use a text editor like nano or vim; in Windows, Notepad can be used, ensuring to select "All Files" and type the filename as .pypirc.

Publish a package

To publish a package with Twine, first identify the path where your package files (.tar.gz and .whl) are located, typically in the dist/ directory created after building your package.

python3 -m twine upload --repository gitea /path/to/files/*

 Replacing /path/to/files/* with the actual path to your package files.

Warning

If a package of the same name and version already exists, you'll need to delete the existing one before publishing the new version.

Result

Once published, the package will be accessible within the "Packages" section of the organization in Gitea.
The following picture displays how the "requests" package appears in Gitea after being pushed to the package registry.

image-2024-2-9_9-18-18.png