py2exe+wxPythonでmyole4axが見つからないと言われる問題

新規にPython環境セットアップして、何はともあれ既存のファイルに対してpy2exeをやろうとしてはまったメモ

py2exeでexe化したファイルを実行すると、下記のようなエラーが発生する。Pythonのバージョンは2.5と2.6のどちらでも再現。

  File "zipextimporter.pyo", line 82, in load_module
  File "wx\lib\flashwin.pyo", line 15, in <module>
  File "zipextimporter.pyo", line 82, in load_module
  File "wx\lib\activex.pyo", line 44, in <module>
ImportError: cannot import name myole4ax

myole4axって何ぞ?

ファイル名  C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\lib\activex.py
44行目

from comtypes.gen import myole4ax

C:\Python25\Lib\site-packages\comtypes\comtypesフォルダはあるけど、C:\Python25\Lib\site-packages\comtypes\comtypes\genフォルダがありませんよ…

wx\lib\activex.pyの該当部分の周りを見てみると、

import wx

import ctypes as ct
import ctypes.wintypes as wt
import comtypes
import comtypes.client as cc
import comtypes.hresult as hr

import sys, os
if not hasattr(sys, 'frozen'):
    f = os.path.join(os.path.dirname(__file__), 'myole4ax.tlb')
    cc.GetModule(f)
from comtypes.gen import myole4ax

ということで、cc.GetModule(f)が実行されると、comtypes.genが生成されるってことなんですね。つまりpy2exeでexeにする前に一回実行しておく必要あり。

py2exeでは実行はせずにファイルをパッケージ化しているだけだから、生成されないままパッケージ化されてしまうというわけ。

もしくはこんな風に手で実行してもOK。

>>> import wx
>>>
>>> import ctypes as ct
>>> import ctypes.wintypes as wt
>>> import comtypes
>>> import comtypes.client as cc
>>> import comtypes.hresult as hr
>>>
>>> import sys, os
>>> f = os.path.join(r"C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\lib", 'myole4ax.tlb')
>>> cc.GetModule(f)
# Generating comtypes.gen._99AB80C4_5E19_4FD5_B3CA_5EF62FC3F765_0_1_0
# Generating comtypes.gen._00020430_0000_0000_C000_000000000046_0_2_0
# Generating comtypes.gen.stdole
# Generating comtypes.gen.myole4ax
<module 'comtypes.gen.myole4ax' from 'c:\Python25\lib\site-packages\comtypes\gen
\_99AB80C4_5E19_4FD5_B3CA_5EF62FC3F765_0_1_0.py'>
>>> 

comtypes.gen.ShockwaveFlashObjectsも同じようにエラーが出る場合があるので、同じ手段で対処できます。


新しく会社作りました!

コメントをどうぞ