twine
repository·main·Indexed 23 days ago
https://github.com/pypa/twineA 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).
What's inside twine
- 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.
Quickstart: Uploading Python packages
mainFollow 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.
- Build your distributions: Use a build frontend like
buildto create your source and binary artifacts. - Upload to Test PyPI: Use the
-r testpypiflag to target the test repository. - 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.
- Build your distributions: Use a build frontend like
Use Keyring for secure credential storage
mainInstead of entering passwords manually, Twine can use the
keyringlibrary to store and retrieve credentials securely.To set an API token for PyPI, use the
keyring setcommand 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.
Install Twine
mainInstall Twine using
pipto begin publishing Python packages to PyPI or other repositories.pip install twineDisable Keyring
mainIf the presence of Keyring causes unexpected prompts or if you prefer to be prompted for a password manually, you can disable Keyring functionality entirely.
keyring --disableConfigure Proxy Support
mainTwine 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/*Extend twine with custom commands
mainTwine's CLI is extensible. To add a new command to the
twinebinary, you must register an entry point in your package's metadata under the grouptwine.registered_commands.When a command is invoked, twine loads the entry point and calls the resulting function, passing any remaining command-line arguments (the
argsremainder) to it.Implement Trusted Publishing (OIDC) with Resolver
mainTrusted Publishing allows CI systems to authenticate with PyPI using OpenID Connect (OIDC) without long-lived secrets.
When a
Resolverdetects 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:- Fetches the
audiencefromhttps://{repository_domain}/_/oidc/audience. - Detects the OIDC token from the ambient environment (e.g., GitHub Actions).
- Exchanges the OIDC token for a PyPI upload token via
https://{repository_domain}/_/oidc/mint-token. - 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.
- Fetches the
How Repository manages authentication and sessions
mainThe
Repositoryclass encapsulates arequests.Sessionobject.- Authentication: Authentication is set during
__init__. If a username or password is provided, it is stored as a tuple inself.session.auth. If both are empty/None,authis set toNone. - Lifecycle: Users should call
.close()on theRepositoryinstance to properly close the underlyingrequests.Sessionand release resources.
- Authentication: Authentication is set during
Configure Twine via .pypirc file
mainTwine can read repository configuration from a.pypircfile located in your home directory. You can also specify a custom path to a configuration file using the--config-fileoption.Configure Twine via Environment Variables
mainTwine supports configuration through environment variables, which is particularly useful for CI/build servers where creating a.pypircfile is inconvenient. Command-line options take precedence over these variables.Twine CLI Commands
mainTwine 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.