<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Pyinstaller on ポンコツ地方エンジニアの技術ブログ</title><link>https://blog-0847c6.gitlab.io/tags/pyinstaller/</link><description>Recent content in Pyinstaller on ポンコツ地方エンジニアの技術ブログ</description><generator>Hugo -- gohugo.io</generator><language>ja-jp</language><lastBuildDate>Wed, 16 Jun 2021 00:00:00 +0900</lastBuildDate><atom:link href="https://blog-0847c6.gitlab.io/tags/pyinstaller/index.xml" rel="self" type="application/rss+xml"/><item><title>Python で作成したアプリを単一の実行ファイルにする</title><link>https://blog-0847c6.gitlab.io/posts/2021/06/16/python/</link><pubDate>Wed, 16 Jun 2021 00:00:00 +0900</pubDate><guid>https://blog-0847c6.gitlab.io/posts/2021/06/16/python/</guid><description>&lt;p&gt;python で作成したアプリを、他のマシン（ネットに繋がっていなかったり、python のバージョンが異なるマシン）で動作させたかったので、単一の実行ファイルを作成して配布することにしました。&lt;/p&gt;
&lt;h2 id="環境"&gt;&lt;a href="#%e7%92%b0%e5%a2%83" class="header-anchor"&gt;&lt;/a&gt;環境
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;開発環境：Ubuntu&lt;/li&gt;
&lt;li&gt;実行環境：Wind River Linux&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;要は、Ubuntu で作成したアプリを Wind River Linux で動作させたいだけです&lt;/p&gt;
&lt;h2 id="単一実行ファイルの作成"&gt;&lt;a href="#%e5%8d%98%e4%b8%80%e5%ae%9f%e8%a1%8c%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%ae%e4%bd%9c%e6%88%90" class="header-anchor"&gt;&lt;/a&gt;単一実行ファイルの作成
&lt;/h2&gt;&lt;p&gt;&lt;code&gt;pyinstaller&lt;/code&gt; をインストールします。&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ pip install pyinstaller
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;以下のコマンドで単一のファイルが作成できます。&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ pyinstaller sample.py --onefile
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;dist&lt;/code&gt; フォルダ内にバイナリファイルが作成されています。&lt;/p&gt;
&lt;h2 id="動的リンクエラーを回避する"&gt;&lt;a href="#%e5%8b%95%e7%9a%84%e3%83%aa%e3%83%b3%e3%82%af%e3%82%a8%e3%83%a9%e3%83%bc%e3%82%92%e5%9b%9e%e9%81%bf%e3%81%99%e3%82%8b" class="header-anchor"&gt;&lt;/a&gt;動的リンクエラーを回避する
&lt;/h2&gt;&lt;p&gt;上記で作成したファイルを実行して動けば何の問題もないのですが、私の場合、実行環境で以下のようなエラーが表示されました。&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-gdscript3" data-lang="gdscript3"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2841&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;Error&lt;/span&gt; &lt;span class="n"&gt;loading&lt;/span&gt; &lt;span class="n"&gt;Python&lt;/span&gt; &lt;span class="n"&gt;lib&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;/tmp/_MEI1GKxS0/libpython3.8.so.1.0&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;dlopen&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;lib64&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;libm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;so&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="err"&gt;`&lt;/span&gt;&lt;span class="n"&gt;GLIBC_2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;29&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39; not found (required by /tmp/_MEI1GKxS0/libpython3.8.so.1.0)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;これについて、以下の記事が参考になりました。&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://qiita.com/kenmaro/items/60438c1659b5598066df" target="_blank" rel="noopener"
 &gt;https://qiita.com/kenmaro/items/60438c1659b5598066df&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;今回は Ubuntu で作成したものを Wind River で動かしたので、動的リンクでエラーになったようなのですが、&lt;code&gt;StaticX&lt;/code&gt; というツールを使えば静的リンクのみの実行ファイルが作成できるようです。&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/JonathonReinhart/staticx" target="_blank" rel="noopener"
 &gt;https://github.com/JonathonReinhart/staticx&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="staticx-のインストールと実行"&gt;&lt;a href="#staticx-%e3%81%ae%e3%82%a4%e3%83%b3%e3%82%b9%e3%83%88%e3%83%bc%e3%83%ab%e3%81%a8%e5%ae%9f%e8%a1%8c" class="header-anchor"&gt;&lt;/a&gt;StaticX のインストールと実行
&lt;/h3&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ pip install staticx
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;尚、環境によっては &lt;code&gt;patchelf&lt;/code&gt; のインストールも必要みたいです。&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ pip install patchelf-wrapper
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;あとは、以下のようにして実行するだけです。&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;$ staticx &amp;lt;pyinstallerで作成したバイナリファイル&amp;gt; &amp;lt;出力ファイル名&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;これで作成したファイルを使うと、無事に実行できました。&lt;/p&gt;
&lt;h2 id="参考-url"&gt;&lt;a href="#%e5%8f%82%e8%80%83-url" class="header-anchor"&gt;&lt;/a&gt;参考 URL
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://qiita.com/hirohiro77/items/466e411fa41f144c8b2a" target="_blank" rel="noopener"
 &gt;https://qiita.com/hirohiro77/items/466e411fa41f144c8b2a&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://qiita.com/kenmaro/items/60438c1659b5598066df" target="_blank" rel="noopener"
 &gt;https://qiita.com/kenmaro/items/60438c1659b5598066df&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item></channel></rss>