Python パッケージ作成
参考:
- https://heavywatal.github.io/python/packaging.html
- https://docs.python.org/3/tutorial/modules.html
- https://docs.python.org/3/reference/import.html
- https://packaging.python.org/
特に:
pyproject.toml を使う方法
ファイル構成:
bithon/
├── LICENSE
├── README.md
├── pyproject.toml
├── src/bithon/
│ ├── __init__.py
│ └── bithon.py
└── tests/pyproject.toml の書き方
[build-system] では、どのビルドツールを使ってパッケージを作るかを指定する。 書き方はツールごとに決まっている。
例えば Hatchling の場合:
pyproject.toml
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"[project] では、パッケージのメタ情報を記載する:
pyproject.toml
[project]
name = "bithon"
version = "0.0.1"
authors = [
{ name="ymat2", email="yuki.matsuda.r7@dc.tohoku.ac.jp" },
]
description = "Personal python package for bioinformatics"
readme = "README.md"
license = {file = "LICENSE"}
requires-python = ">=3.8"
dependencies = []
[project.urls]
Homepage = "https://ymat2.github.io"
Repository = "https://github.com/ymat2/bithon"name- PyPI 既存のものと被ってはいけないらしいが、git から利用する分には OK?
version-
コードを更新した時は番号を変えないと
pip install --upgradeで更新されない。 dependencies- リスト形式で依存パッケージを記載する。
setup.py を使う方法
setuptools に依存する setup.py を使う方法は現在は非推奨 (Legacy) らしい。 pyproject.toml で一元管理する方法が主流で、Poetry など パッケージ作成をラクに行えるツールがあるっぽい。
ファイル構成:
mython/
├── LICENSE
├── README.md
├── setup.py
├── mython/
│ ├── __init__.py
│ └── mython # 拡張子なしでもいい
└── tests/setup.py
from setuptools import setup
setup(
name = "mython",
version = "0.1.0",
python_requires = ">=3.6",
scripts = ["mython/mython",]
)サブディレクトリありの場合
mython/
├── LICENSE
├── README.md
├── setup.py
├── mython/
│ ├── __init__.py
│ ├── mython
│ └── util/
│ ├── __init__.py
│ ├── bye.py
│ └── hello.py
└── tests/util/__init__.py-
from mython.util import *のような形で非明示的に全部読み込むには、 どのスクリプトを含めるかを書いておかないといけない。 -
__all__ = ["hello", "bye"]