windows7/python3.3/virtualenv/cx_Freeze/Import Error/MAXREPEAT
Pythonを使ってシリアル通信をしよう!となったとき、PyserialとTkinterを使うっていうのはよくある(たぶん)と思います。ただ、pythonで書かれたプログラムをみんなが使えるようにするためにはPythonの環境に依存しない形に変換する必要がありますよね。そこで、cx_Freezeというライブラリを使ってpythonスクリプトをexeファイルに変換します。その時に起こったエラーとその解決方法についてまとめました。
exe化する際にtkinterをimportする場合に発生しがちなエラー
まず、cx_Freezeで.pyのファイルを.exeに変換する過程でエラーが発生して処理がストップします。
tkinterを使用するのに必要なTCL_PARAMETERとTK_PARAMETERがインポートできないっぽいです。
Import Error
TCL_PARAMETER
手動でパスを追加してやれば良いです。僕の場合、virtualenvを使った仮想環境だったので、activate.batに以下の二行を追加してやります。
set "TCL_LIBRARY=C:\Python33\tcl\tcl8.5"
set "TK_LIBRARY=C:\Python33\tcl\tk8.5"
こうすると、”とりあえず”exeファイルを作ることができるようになります。ただ、実行はできません( ◠‿◠ )
exe化したファイルを起動できないケース
もしあなたがある特定のバージョンのpythonを利用していた場合、exeファイルを起動すると以下のエラーが出ます。
Cannot import traceback module.
Exception:cannot import name MAXREPEAT
Original Exception:cannot import name MAXREPEAT
ぐぐってもさっぱりわからず、3時間以上格闘しました。
いろいろ試しましたが、解決方法を先に書いておきます。
解決方法
python3の人はpython3.3.5にアップグレードしましょう。(python2の人はすいませんがわかりません)
MAXREPEATがimportできないのはpython3.3.0のバグなので、アップグレードすれば直ります。
僕がトライした内容
すいません、最初は諸事情で英語で書いたのですが日本語に訳すのが面倒なので英語のままにしておきます←
1. check sre_constants.py, sre_parse.py, sre_compile.py
As the first refferece (bottom of this article) says, I tried to modify sre_constants.py, but the file of python33 was different from that of python27, so I couldn't try this solution. Next, I tried the solution mentioned in the second referrence, but it also didn't work.
2. install cx_Freeze in other ways
- donwload exe file
https://pypi.python.org/pypi?:action=display&name=cx_Freeze&version=4.3.3 - change the python environment ( virtualenv -> local python3 )
The error message changed to the following one
http://stackoverflow.com/questions/8597126/cx-freeze-ldap-importerror-dll-load-failed-1-is-not-a-valid-win32-application
First, I tried installing "cx_Freeze-4.3.3.win32-py3.3.exe", but it didn't work because my computer is 64bit, not 32bit.
Second, I tried installing "cx_Freeze-4.3.3.win32-py3.3.exe" but it didn't work too. The error message of the second one is the same as the mentioned in the top of this article.
Third, I downloaded "cx_Freeze-4.3.3-cp33-none-win_amd64.whl" and checked some examples.
3. change the version of Python
the version of my Python3 was 3.3.0, so I changed it to 3.3.5, then it worked...