0
1

More than 1 year has passed since last update.

爆速化するためにpythonからRustを呼び出そう!

Last updated at Posted at 2022-02-05

Rustで作成したライブラリをpythonで呼ぶ方法

PyO3を利用するには、ツールチェインのnightly版をインストールする必要があります。

rustup install nightly 
rustup default nightly 
rustup toolchain list

Cargo.tomlにPyO3の依存関係を追記します。

[lib]
name = "eratosthenes"
crate-type = ["cdylib"]

[dependencies.pyo3]
version = "0.13.2"
features = ["extension-module"]

ダイナミックライブラリを作成するために、--releaseをつけてビルドします。

cargo build --release

もし以下の様にエラーが出る場合は、バージョンを新しくすることで解決できる可能性があります。
参考:https://github.com/rust-osdev/x86_64/issues/234

#![cfg_attr(feature = "const_fn", feature(const_in_array_repeat_expressions))]
                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ feature has been removed

cargo build --releaseを実行すると、target/release内にWindowsであれば.dll,
Linuxであれば.so、Macであれば.dylibが作成されます。
作成されたファイルを以下対応拡張子に変更し、利用するpythonスクリプトと同じディレクトリに格納することでpythonで利用することができます

OS --releaseで作成されるファイル pythonで利用するために
Windows .dll .pyd
Mac .dylib .so
Linux .so .so

実装動画

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1