Skip to content

Instantly share code, notes, and snippets.

@linhpduc
Last active June 29, 2023 18:21
Show Gist options
  • Save linhpduc/d4e9d4c6f1de6bc159db1bf11c1f7793 to your computer and use it in GitHub Desktop.
Save linhpduc/d4e9d4c6f1de6bc159db1bf11c1f7793 to your computer and use it in GitHub Desktop.
[Howto] Packaging python projects

Install/update pip (python package manager)

python3 -m pip install --upgrade pip

Create a project with the structure same like this

packaging_pypa
├── LICENSE
├── README.md
├── pyproject.toml
├── src
│   ├── eat-the-whole-world
│   │   ├── __init__.py
│   │   └── factorial.py
└── tests
  • packaging_pypa: working directory
  • README.md: say something about your project
  • LICENSE: tells users who install your package the terms under which they can use your package
  • tests/: test files
  • src: contains package source code,
    • eat-the-whole-world: name of package, it should be unique that doesn’t conflict with packages uploaded by other people
  • pyproject.toml: project metadata, here is a sample
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "eat-the-whole-world"
version = "0.0.1"
authors = [
  { name="linhpduc", email="<real_email>" },
]
description = "A package can eat the whole world"
readme = "README.md"
requires-python = ">=3.9"
classifiers = [
    "Programming Language :: Python :: 3",
    "License :: OSI Approved :: MIT License",
    "Operating System :: OS Independent",
]

Create distribution archives

python3 -m pip install --upgrade build
python3 -m build

Goto PyPI or TestPyPI website to reg an account and create a API Token. Save API token to file ~/.pypirc:

[testpypi]
  username = __token__
  password = <real_token>

Install twine, a tool for publishing packages

python3 -m pip install --upgrade twine

Upload all of the archives

python3 -m twine upload --repository testpypi dist/*

And ... let's rock!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment