Publishing with twine
Upload to PyPI.
Publishing with twine is a free Python Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Final Step
You have built artifacts in dist/. The last step is uploading them to PyPI so the world can pip install your package. The standard tool for this is twine.
twine uploads securely over HTTPS and validates artifacts before sending.
Installing twine
Install it with pip install twine. It is a small, focused tool: its job is purely to validate and upload the files in dist/. It does not build anything.
TestPyPI First
There is a separate sandbox, TestPyPI, for rehearsing uploads. Publish there first to catch problems without polluting the real index. Names and versions on TestPyPI are independent of PyPI.
Upload with twine upload --repository testpypi dist/*.
Checking Before Upload
twine check dist/* validates that your metadata and long description will render correctly on PyPI. Run it every time; a broken README description can otherwise show as plain text on your project page.
files = ['mytool-0.1.0.tar.gz', 'mytool-0.1.0-py3-none-any.whl']
for f in files:
status = 'PASSED' if f.endswith(('.gz', '.whl')) else 'SKIP'
print('check', f, '->', status)API Tokens, Not Passwords
Authenticate with an API token from your PyPI account, not your password. Use __token__ as the username and the token (starting with pypi-) as the password. Tokens can be scoped to a single project and revoked anytime.
username = '__token__'
token = 'pypi-AgEIcHl...'
print('Username:', username)
print('Token starts with:', token[:5])
print('Never commit the token to git')Storing Credentials
Avoid retyping tokens by storing them in a ~/.pypirc file or environment variables (TWINE_USERNAME, TWINE_PASSWORD). Keep this file out of version control and readable only by you.
Uploading to PyPI
Once TestPyPI looks good, publish for real with twine upload dist/*. Within moments your package is installable worldwide. There is no undo: you cannot re-upload the same version after deleting it.
Versions Are Immutable
PyPI never lets you overwrite a released version. If you find a bug after publishing, you must release a new version (for example bump the patch). Build the rule into your workflow so you never get stuck.
published = {'1.0.0', '1.0.1'}
new_version = '1.0.1'
if new_version in published:
print(new_version, 'already on PyPI - bump and rebuild')
else:
print(new_version, 'is free to upload')Verify the Install
After uploading, confirm it works: create a fresh virtual environment and run pip install mytool, then import it. Installing from a clean environment catches missing files and dependency mistakes the build did not.
Trusted Publishing
Modern projects can skip manual tokens with trusted publishing: PyPI authenticates a CI workflow (such as GitHub Actions) directly via OpenID Connect. No long-lived secret is stored anywhere, which is more secure.
twine still does the upload, but the credentials are short-lived and issued at run time.
Automating Releases
A common setup triggers a release when you push a git tag like v1.0.0. The CI workflow builds, runs twine check, and uploads automatically. Tagging becomes the single action that ships a version.
This keeps releases consistent and removes manual upload mistakes.
Quick Check
Test your publishing knowledge.
Recap
You published a package:
pip install twine, thentwine check dist/*- Rehearse on TestPyPI before the real upload
- Authenticate with a scoped API token (
__token__), never a password twine upload dist/*publishes; versions are immutable, so bump to fix bugs- Verify with a clean
pip install
Frequently asked questions
Is the “Publishing with twine” lesson free?
Yes — the full text of “Publishing with twine” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “Publishing with twine”?
Upload to PyPI. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Publishing with twine” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Project Structure and pyproject.toml
- Building with build
- Versioning and Metadata
- Publishing with twine