Skip to main content

Package with setuptools


Directory Structure

<project>/
├── pyproject.toml
├── LICENSE
├── README.md
└── <package>/
├── __init__.py # import <package>
├── py.typed # for type hint
├── <subpackage>/
│ ├── __init__.py # from <package> import <subpackage>
│ ├── <module>.py # from <package>.<subpackage> import <module>
│ └── ...
└── ...

pyproject.toml

pyproject.toml
[project]
name = "<packageName>" # pip install <packageName>, <package>와 다를 수 있습니다.
version = "<version>"
description = "<description>"
authors = [{ name = "Hyeonki Hong", email = "[email protected]" }]
requires-python = ">=3.11"

licencse = {file = "LICENSE"}
readme = "README.md"
keywords = []
classifiers = [
"Programming Language :: Python :: 3",
"Intended Audience :: Developers",
]

[project.urls]
homepage = "https://wiki.loliot.net"
repository = "https://github.com/hhk7734/example"
documentation = "https://wiki.loliot.net"

[tool.setuptools]
zip-safe = false

[tool.setuptools.package-data]
"*" = ["py.typed", "*.pyi"]

[tool.setuptools.packages.find]
# where = ["src"]
include = ["<package>", "<package>.*"]

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

version

Dynamic version with setuptools_scm

pyproject.toml
[project]
# version = "<version>"
dynamic = ["version"]

# version을 git tag로부터 가져옵니다.
[tool.setuptools_scm]
# .git 이 있는 경로
# root = "."
# 버전을 찾는 정규식
# tag_regex = '^(?:[\w-]+-)?(?P<version>[vV]?\d+(?:\.\d+){0,2}[^\+]*)(?:\+.*)?$'
# tag를 찾는 명령어
# git_describe_command = 'git describe --dirty --tags --long --match "*[0-9]*"'

[build-system]
requires = ["setuptools", "setuptools_scm"]
build-backend = "setuptools.build_meta"

__version__

<package>/__init__.py
from importlib.metadata import version, PackageNotFoundError

try:
__version__ = version("<packageName>")
except PackageNotFoundError:
# package is not installed
pass

Dependencies

pyproject.toml
[project]
dependencies = []

Scripts

pyproject.toml
[project.scripts]
<cli> = "<package>.<module>" # __main__
<cli> = "<package>.<module>:<function>"

Build

python3 -m pip install -U build
python3 -m build

Publish

python3 -m pip install twine

Test PyPI

python3 -m twine upload --repository testpypi dist/*
python3 -m pip install \
--index-url https://test.pypi.org/simple/ \
--verbose \
--no-deps \
<packageName>

PyPI

python3 -m twine upload dist/*
python3 -m pip install <packageName>