以下の内容はhttps://nikkie-ftnext.hatenablog.com/entry/try-maturin-pypi-upload-confuse-binary-each-environmentより取得しました。


Rust プログラムから作ったバイナリを PyPI にアップロードしてみて

はじめに

七尾百合子さん、お誕生日 178日目 おめでとうございます! nikkieです。

過去にこんな記事を書きました

今回はやっていませんが、PyPIにもpublishできそうに思います。(Rustプログラムから作ったバイナリを、maturinでPythonパッケージにする!

こちらに取り組みました。

目次

できたこと

https://pypi.org/project/sampleproject-rs/0.0.2/

今は GitHub Codespaces に限り動きます1(未確認ですが Ubuntu 環境は動くと期待されます)

$ pipx run --spec sampleproject-rs sample
Call your main application code here

sampleproject リスペクトです。
こちらの Rust バイナリ版を用意しようと思い付きました。

pyproject.toml は過去記事から変わりません。
sampleproject の性質からパッケージメタデータを網羅しています。

GitHub Actions で PyPI にアップロード2します3
https://github.com/ftnext/sampleproject-rs/blob/0.0.2/.github/workflows/publish.yml#L20-L25

知ったこと

Pythonの世界観

sampleproject-rs は、macOS(M1 Mac)だと動きません

% uvx --from sampleproject-rs sample
  × No solution found when resolving tool dependencies:
  ╰─▶ Because only sampleproject-rs==0.0.1 is available and
      sampleproject-rs==0.0.1 has no wheels with a matching platform tag
      (e.g., `macosx_14_0_arm64`), we can conclude that all versions of
      sampleproject-rs cannot be used.

      hint: Wheels are available for `sampleproject-rs` (v0.0.1) on the
      following platform: `manylinux_2_34_x86_64`

これはバイナリを GitHub Actions (Ubuntu環境)でビルドしたためという理解です。
Pythonって色々言われる言語ですが、Pythonさえ入っていればPython製パッケージはOSの違いによらず動くというのはすごいことと再認識しました。
Astral社は結構地道な配布作業をしています(自動化してますが)
https://github.com/astral-sh/ruff/blob/0.13.0/.github/workflows/build-binaries.yml

maturinまわり

バイナリが環境固有ということに気づいて調べる中で知った事項です(これから適用するものも含む)

www.maturin.rs

maturinのコマンド

  • maturin build--sdist--out dist
  • maturin new
  • maturin publish
  • maturin generate-ci github

maturinのGitHub Actions

宿題事項

理解して採用したいサンプル(maturin-action の Examples より)

こちらをパクると macOS 向けにパッケージ配布できそうです

終わりに

Rust プログラムから作ったバイナリを maturin で Python パッケージにビルドし、PyPI にアップロードしました。
普段の Python 製パッケージとは対照的に、ビルド環境と異なる環境では動かないという体験は新鮮でした。
maturin はもっと使いこなせると思うので、先行例を参考により多くの環境に配布できるようもう少し手を動かしてみます。


  1. GitHub Actions や Codespaces のイメージには pipx がインストールされています
  2. Trusted Publisher に対応しています。



以上の内容はhttps://nikkie-ftnext.hatenablog.com/entry/try-maturin-pypi-upload-confuse-binary-each-environmentより取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14