LoginSignup
4
5

More than 3 years have passed since last update.

Pythonをバイナリファイルに変換する「PyOxidizer」を使ってみた。

Last updated at Posted at 2019-07-18

Pythonファイルをマルチプラットフォームでバイナリファイル化できるという「PyOxidizer」を試してみました。
EV3上で実行できるのかも試しています。

実験環境

  • Windows10
  • Ubuntu 18.04

参考

1. Rustをインストールする

PyOxidizerはRust言語を利用しているため、インストールする必要があります。
- Windowsの場合

1. Visual C++ Build Tools のインストール
RustをWindowsで使用する際、C++コンパイラーが別途必要があるので、インストールしておきます。

Visual C++ Build Tools をクリックしてBuildToolsをダウンロードします。
ダウンロードしファイルを実行し、Visual C++ Build Tools にチェックを入れてインストールします。
2. Rustupを使ってRustをインストールします。バージョンやターゲットの変更の容易さからRustupを使ってのインストールが推奨されています。

Rustup から rustup‑init.exe のリンクをクリックし、ダウンロードします。
ダウンロードした rustup‑init.exe を実行すると以下の画面が表示されます。
1を入力して、エンターキーを押し、インストールを開始します。

Welcome to Rust!

This will download and install the official compiler for the Rust programming
language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to Cargo's bin
directory, located at:

C:\Users\[ユーザー名]\.cargo\bin

This path will then be added to your PATH environment variable by modifying the
HKEY_CURRENT_USER/Environment/PATH registry key.

You can uninstall at any time with rustup self uninstall and these changes will
be reverted.

Current installation options:

default host triple: x86_64-pc-windows-msvc
default toolchain: stable
modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

以下のような画面になったらインストールが完了しています。また、PATHの登録も行われています。

Rust is installed now. Great!

To get started you need Cargo's bin directory (%USERPROFILE%\.cargo\bin) in
your PATH environment variable. Future applications will automatically have the
correct environment, but you may need to restart your current shell.

Press the Enter key to continue.

コマンドプロンプトを開き、正しくインストールされているか確認します。

$>rustc --version
rustc 1.36.0 (a53f9df32 2019-07-03)
$>cargo --version
cargo 1.36.0 (c4fcfb725 2019-05-15)

  • Linux(Ubuntu)の場合
  • ターミナルを開き、以下のコマンドを実行します。
$ curl https://sh.rustup.rs -sSf | sh

すると以下の画面が表示されます。
1を入力して、エンターキーを押し、インストールを開始します。

info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust programming 
language, and its package manager, Cargo.

It will add the cargo, rustc, rustup and other commands to Cargo's bin 
directory, located at:

/home/yoshi/.cargo/bin

This path will then be added to your PATH environment variable by modifying the
profile file located at:

/home/yoshi/.profile

You can uninstall at any time with rustup self uninstall and these changes will
be reverted.

Current installation options:

default host triple: x86_64-unknown-linux-gnu
default toolchain: stable
modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

以下のような画面になったらインストールが完了しています。

Rust is installed now. Great!

To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH 
environment variable. Next time you log in this will be done automatically.

To configure your current shell run source $HOME/.cargo/env
  1. Rustを使用するにはCargoのbinディレクトリを環境変数PATHに追加する必要があります。

パスを追加するスクリプトが容易されているので実行します。

$ source $HOME/.cargo/env

コマンドプロンプトを開き、正しくインストールされているか確認します。

$>rustc --version
rustc 1.36.0 (a53f9df32 2019-07-03)

$>cargo --version
cargo 1.36.0 (c4fcfb725 2019-05-15)

2. PyOxidizerのインストール

cargoを使ってPyOxidizerをインストールします。

cargo install pyoxidizer

しばらく時間がかかるので待ちます。

以下の表示がでれば、インストールが完了しています。

  • Windows
Finished release [optimized] target(s) in 11m 28s
Installing C:\Users\ユーザー名\.cargo\bin\pyoxidizer.exe
Installed package `pyoxidizer v0.2.0` (executable `pyoxidizer.exe`)

インストール後、一度コマンドを実行して確認します。

$>pyoxidizer --version
PyOxidizer 0.2.0
  • Linux
Finished release [optimized] target(s) in 12m 58s
Installing /home/ユーザー名/.cargo/bin/pyoxidizer
Installed package `pyoxidizer v0.2.0` (executable `pyoxidizer`)

インストール後、一度コマンドを実行して確認します。

Linuxの場合は、パスを追加する必要があります。

$>PATH="$PATH:/home/ユーザー名/.cargo/bin/"
$>source ~/.profile
$>pyoxidizer --version
PyOxidizer 0.2.0

3. First PyOxidizer Project

PyOxidizer用のプロジェクトを作成します。

$>pyoxidizer init pyapp
$>cd pyapp

この状態で以下のコマンドを実行すると、Pythonのインタプリタが起動します。

$>pyoxidizer run

※この時、コンソールの文字コードがUTF-8ではない場合、エラーが発生します。

その場合以下のコマンドを実行して文字コードを変更してください。

$>chcp 65001

Windowsの場合、文字コードは起動ごとにリセットされるため、注意しましょう。

4. Pythonコードのバイナリ化

お約束のHelloWorldをバイナリ化してみます。

「helloworld.py」を作成し、pyappフォルダ直下に置きます。

「pyapp/pyoxidizer.toml」が設定ファイルとなっているので、編集します。

mode = 'repl'をコメントアウトし、その下2行を追記します。

[[embedded_python_run]]
# mode = "repl"         
mode = "module"         
module = "helloworld"

下記の部分のコメントアウトを外し、package名を「helloworld」に変えます。

[[packaging_rule]]
type = "package-root"
path = "."
packages = ["helloworld"]

PyOxidizerを実行します。

$>pyoxidizer run
~
HelloWorld

runオプションはビルドして実行なので、以下にビルドされたファイルが生成されています。
pyoxidizer\pyapp\build\apps\pyapp
- pyapp.exe (windows)
- pyapp (linux)
が出力されています。

余談

公式ドキュメントに出力ターゲットが変更できると書かれていたので、Windows環境にて以下を試してみました。

pyoxidizer build --target x86_64-pc-windows-msvc
pyoxidizer build --target x86_64-apple-darwin
pyoxidizer build --target x86_64-unknown-linux-gnu
pyoxidizer build --target x86_64-unknown-linux-musl

結果はWindowsターゲット以外は、環境ファイル依存のようなエラーが出てしまいました。

設定を変更したり、ファイルを追加することで対応できるかもしれませんが、今回は見送ります。

さて、linux用のビルドファイルですが、GNU用とUbuntu用のものです。

もちろんEV3では動かないのですが、ものは試しでやってみましたが、ディストリビューションエラーが出てしまい実行はできませんでした・・・

4
5
0

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
4
5