DEV Community

Cover image for Guide to publish your first PyPI library ๐Ÿš€
Nitin Kumar
Nitin Kumar

Posted on

Guide to publish your first PyPI library ๐Ÿš€

๐Ÿ‘€ Do you also want to be included in that elite, mysterious, and totally brag-worthy list of contributors for a public PyPI package?
Yes? No? Still scrolling like itโ€™s an Instagram reel? ๐Ÿ“ฑ
Cool, letโ€™s fix that.


Why to create a py package? ๐Ÿค”๐Ÿ

Before you start dreaming about PyPI fame (and GitHub stars ๐ŸŒŸ that your relatives wonโ€™t understand), pause for a reality check.

First, decide if your project is actually PyPI-worthy. Before touching configs, sanity-check that your project is reusable (no hardcoded paths, credentials, or โ€œworks only on my laptopโ€ local files ๐Ÿ’ปโŒ) & has a clear entry point (functions/CLI).

If your script is โ€œrun once and forgetโ€ โ€” PyPI is overkill ๐Ÿ’€
If others (or future-you, after 3 months of memory loss ๐Ÿง ) can reuse it โ†’ publish it ๐Ÿš€

Think of it like the World Cup ๐Ÿ โ€” not every gully cricketer makes it to the squad, but the good ones do ๐Ÿ˜‰
Same rules apply here.


๐Ÿ“‚ Folder structure needed (yes, structure matters)

Just like Indiaโ€™s budget announcements ๐Ÿ“Š โ€” boring, structured, but very important.

my_package/
โ”‚
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ my_package/
โ”‚       โ”œโ”€โ”€ __init__.py
โ”‚       โ”œโ”€โ”€ core.py
โ”‚       โ””โ”€โ”€ utils.py
โ”‚
โ”œโ”€โ”€ tests/
โ”‚   โ””โ”€โ”€ test_basic.py
โ”‚
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ MANIFEST.in
โ””โ”€โ”€ .gitignore
Enter fullscreen mode Exit fullscreen mode

Mess this up, and PyPI will treat your package the way Twitter treats breaking news โ€” harshly ๐Ÿ˜ฌ๐Ÿฆ.


Content of pyproject.toml โค๏ธโ€๐Ÿ”ฅ

This is the heart of deployment.
Mess this up and your package dies faster than a government promise during elections ๐Ÿ—ณ๏ธ๐Ÿ˜Œ.

[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "my-package"
version = "0.1.0"
description = "Utility to validate API responses using schema rules"
readme = "README.md"
license = { file = "LICENSE" }
authors = [
    { name = "FIRSTNAME LASTNAME", email = "your@email.com" }
]
requires-python = ">=3.8"

dependencies = [
]

[project.urls]
Homepage = "https://github.com/yourname/my-package"
Enter fullscreen mode Exit fullscreen mode

Yes, every line matters.
No, PyPI doesnโ€™t care if โ€œit worked locallyโ€ ๐Ÿ˜ŒโŒ.


Contents of MANIFEST.in (donโ€™t skip ๐Ÿ™)

Ensures non-code files are packaged.
Skipping this is like hosting the World Cup final without a trophy ๐Ÿ† โ€” technically possible, emotionally devastating.

include README.md
include LICENSE
Enter fullscreen mode Exit fullscreen mode

Without this, your PyPI page may break.
And users will judge you silently ๐Ÿค.


Key rules ๐Ÿšจ

There are some key rules while uploading your package on the PyPI website. These are non-negotiable โ€” like ICC rules during finals ๐Ÿ˜ค๐Ÿ:

  1. Name must be unique on PyPI (yes, utils, helpers, test-lib are already taken ๐Ÿ˜‘)
  2. Version must follow semver
  3. Once uploaded โ†’ cannot overwrite the same version (PyPI never forgets ๐Ÿ˜ˆ)

Requirements ๐Ÿ“‹

  1. A sample project which should adhere to these conditions
  2. Any code editor. Iโ€™m using VS Code here (because obviously ๐Ÿ’™)
  3. A valid package name should be unique โš ๏ธ
  4. A PyPI & test.pypi Account

; PyPI account can be created from here & tutorial for the same is in this youtube video

TestPyPI account can be created from here & tutorial for the same is in this youtube video

Yes, two accounts.
No, PyPI doesnโ€™t trust you immediately โ€” neither should it ๐Ÿ˜Œ๐Ÿ”.


Steps to create & upload ๐Ÿš€

Install build tools ๐Ÿ› ๏ธ

pip install --upgrade build twine
Enter fullscreen mode Exit fullscreen mode
  • build โ†’ creates wheel & source
  • twine โ†’ uploads to PyPI (your final boss ๐ŸŽฎ)

Build your package ๐Ÿ“ฆ

python -m build
Enter fullscreen mode Exit fullscreen mode

If this executes successfully (pray to the Python gods ๐Ÿ๐Ÿ™), it will create:

dist/
โ”œโ”€โ”€ my_package-0.1.0-py3-none-any.whl
โ””โ”€โ”€ my_package-0.1.0.tar.gz
Enter fullscreen mode Exit fullscreen mode

If it fails โ€” congrats ๐ŸŽ‰, youโ€™re officially learning ๐Ÿคจ


Upload to testPyPI (optional but smart ๐Ÿง )

twine upload --repository testpypi dist/*
Enter fullscreen mode Exit fullscreen mode

Then install your uploaded package from testPyPI:

pip install --index-url https://test.pypi.org/simple/ my-package
Enter fullscreen mode Exit fullscreen mode

Test import + functionality here.
Break things now, not in production ๐Ÿ˜ฌ๐Ÿ”ฅ.


Upload to real PyPI ๐ŸŒ

twine upload dist/*
Enter fullscreen mode Exit fullscreen mode

; make sure dist/* uploads everything inside dist/.

And boom ๐Ÿ’ฅ โ€” Congratulations, youโ€™ve just uploaded your own package! ๐ŸŽ‰๐ŸŽ‰
Take a moment. Screenshot it. Flex on LinkedIn ๐Ÿ˜Ž.


Something more ๐Ÿ‘€

Common mistakes โŒ

โŒ Hardcoded paths
โŒ Forgot README / LICENSE
โŒ Version conflict
โŒ Import errors due to bad structure
โŒ Uploading secrets accidentally (this one hurts the most ๐Ÿ’€๐Ÿ”)


Pro tips ๐Ÿ’ก

โœ… Add CLI using entry-points
โœ… Automate release via GitHub Actions
โœ… Tag versions (v0.1.0)
โœ… Add __version__ inside package
โœ… Use ruff / black before publishing

Clean code = happy users ๐Ÿ˜Š.


Hope youโ€™ve successfully created your first package on PyPI ๐Ÿ๐ŸŽฏ
Do share your package name in comments โ€” letโ€™s hype each other like World Cup fans ๐Ÿ‡ฎ๐Ÿ‡ณ๐Ÿ.

Iโ€™ve recently committed pyshrink. Do check it out ๐Ÿ‘€โœจ.

Also, if youโ€™ve got questions, confusion, or existential doubts about packaging โ€” comment it out ๐Ÿ’ฌ๐Ÿ˜„.


Author
๐Ÿ˜Ž Nitin Kumar

Top comments (0)