LoginSignup
58
70

More than 5 years have passed since last update.

Pyinstaller で Python スクリプトを Windows で実行可能な .exe にする

Last updated at Posted at 2015-07-04

Python スクリプトを Windows で実行可能な .exe にする

これまでは、py2exe や CX_Freeze など、setup.py をいちいち書かないといけませんでした。最近は PyInstaller がオススメということらしいので試してみた。

インストール

pip install pyinstaller

対象スクリプト

C:\Users\kounoike\Documents\python\installer-test>type hello.py
#!python
# coding: utf-8

print "hello world"

C:\Users\kounoike\Documents\python\installer-test>python hello.py
hello world

.exe の作成

pyinstaller hello.py

これだけ。面倒な setup.py の記述も何もなし。dist\hello 以下に hello.exe と必要なライブラリ群が出来上がる。

問題点

どうも py2exe などに比べて過剰にパッケージを取り込んでしまうようで、

#!python
# coding: utf-8

import matplotlib.pyplot as plt

print "hello world"

と書き換えただけで、dist 以下が100MBにも膨れ上がる。見てみると、IPython とか、QT4、OpenGLなど余計なものがまぎれこんでいるのがわかる。

そこで virtualenv ですよ。

というわけで、virtualenv を使いましょう。http://qiita.com/kounoike/items/14bfd7c328be0d69c770 などを参考にしてください。

PyInstaller を virtualenv で使う際の注意点は一つ。pywin32 を easy_install で入れておきましょう。

追記@2015/07/21

あれ、やっぱり日本語フォルダに置くとうまく動かない・・・

  • ネットワークドライブの日本語フォルダ作ってそのフォルダをシフト右クリックしてコマンドプロンプトで開く→大丈夫
  • デスクトップに日本語フォルダを作ってそのフォルダをシフト右クリックしてコマンドプロンプトで開く→ダメ

なんでネットワークドライブだと大丈夫なんだろう?

58
70
2

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
58
70