twine

repository·main·Indexed 23 days ago

https://github.com/pypa/twine

A command-line utility for securely uploading Python distribution artifacts (source and binary) to the Python Package Index (PyPI) and other package repositories. It includes tools to validate package metadata via `twine check` and upload distributions via `twine upload`, with support for .pypirc configuration, environment variables, keyring integration, and Trusted Publishing (OIDC).

Tokens
6.2K
Snippets
7
Records
64
Agent score
82%

What's inside twine

  1. What is Twine

    main
    Twine is a utility used for publishing Python packages to PyPI (the Python Package Index). It allows for build-system-independent uploads of both source and binary distribution artifacts for new and existing projects.
  2. Quickstart: Uploading Python packages

    main

    Follow these steps to build and upload your distributions. It is recommended to upload to Test PyPI first to verify your package before releasing it to the main PyPI repository.

    1. Build your distributions: Use a build frontend like build to create your source and binary artifacts.
    2. Upload to Test PyPI: Use the -r testpypi flag to target the test repository.
    3. Upload to PyPI: Once verified, upload to the production repository.

    Note: When prompted for credentials, Twine will not display characters as you type your password.

  3. Use Keyring for secure credential storage

    main

    Instead of entering passwords manually, Twine can use the keyring library to store and retrieve credentials securely.

    To set an API token for PyPI, use the keyring set command with the PyPI upload URL and the __token__ username.

    Note for Linux users: In headless environments (like servers), additional steps may be required to ensure Keyring can store secrets securely.

  4. Configure Proxy Support

    main

    Twine can use a proxy by setting standard proxy environment variables. You can set these for the specific Twine command without exporting them to your entire shell session.

    HTTPS_PROXY=socks5://user:pass@host:port twine upload dist/*
  5. Extend twine with custom commands

    main

    Twine's CLI is extensible. To add a new command to the twine binary, you must register an entry point in your package's metadata under the group twine.registered_commands.

    When a command is invoked, twine loads the entry point and calls the resulting function, passing any remaining command-line arguments (the args remainder) to it.

  6. Implement Trusted Publishing (OIDC) with Resolver

    main

    Trusted Publishing allows CI systems to authenticate with PyPI using OpenID Connect (OIDC) without long-lived secrets.

    When a Resolver detects that it should use Trusted Publishing (e.g., when the username is __token__ on PyPI and no explicit password is provided), it performs the following steps:

    1. Fetches the audience from https://{repository_domain}/_/oidc/audience.
    2. Detects the OIDC token from the ambient environment (e.g., GitHub Actions).
    3. Exchanges the OIDC token for a PyPI upload token via https://{repository_domain}/_/oidc/mint-token.
    4. Caches the token and automatically renews it if it is within 5 minutes of expiration (TOKEN_RENEWAL_THRESHOLD).

    If the environment does not support OIDC, it falls back to prompting for an API token.

  7. How Repository manages authentication and sessions

    main

    The Repository class encapsulates a requests.Session object.

    • Authentication: Authentication is set during __init__. If a username or password is provided, it is stored as a tuple in self.session.auth. If both are empty/None, auth is set to None.
    • Lifecycle: Users should call .close() on the Repository instance to properly close the underlying requests.Session and release resources.
  8. Configure Twine via .pypirc file

    main
    Twine can read repository configuration from a .pypirc file located in your home directory. You can also specify a custom path to a configuration file using the --config-file option.
  9. Configure Twine via Environment Variables

    main
    Twine supports configuration through environment variables, which is particularly useful for CI/build servers where creating a .pypirc file is inconvenient. Command-line options take precedence over these variables.
  10. Twine CLI Commands

    main

    Twine provides two primary commands for managing package distributions:

    • twine upload: Uploads one or more distributions to a specified repository.
    • twine check: Validates whether your distribution's long description will render correctly on PyPI.