コンテンツにスキップ

Pants で whl を作る

はじめに

Pants のpython_distributionターゲットで、Python パッケージを配布用の whl ファイルにまとめる方法を示す。BUILDファイルの設定と、生成するコマンドを紹介する。

作り方

src/foo/bar/BUILD
python_distribution(
    name="mydist",
    dependencies=[
        ":pyproject",
        "src/foo/bar:src",
        # Dependencies on code to be packaged into the distribution.
    ],
    provides=python_artifact(
        name="mydist",
    ),
    # Example of setuptools config, other build backends may have other config.
    wheel_config_settings={"--global-option": ["--python-tag", "py37.py38.py39"]},
    # Don't use setuptools with a generated setup.py.
    # You can also turn this off globally in pants.toml:
    #
    # [setup-py-generation]
    # generate_setup_default = false
    generate_setup = False,
)

上記の例では、pants package src/foo/bar:mydistコマンドで whl を作れる。このmydistターゲットを他のターゲットの依存関係に含めると whl を使えるので、Docker の場合は次のようにdocker_imageのdependenciesへ含めれば、COPY xxx.whl .のようにして whl をコピーできる。

src/foo/bar/BUILD
1
2
3
4
5
6
7
8
docker_image(
    name="myimage",
    dependencies=[
        "src/foo/bar:mydist",
        # Dependencies on other images.
    ],
    # ...
)

参考