3

win32comライブラリを使用してiTunesを制御するプログラムがありますが、実行可能ファイルにコンパイルする際に問題が発生しています。問題は、DispatchWithEventsの代わりにを使用することを中心に展開しているようですDispatch。私は自分の問題を説明するために非常に簡単なプログラムを作成しました:

import win32com.client
win32com.client.gencache.is_readonly = False #From py2exe wiki

class ITunesEvents(object):
    def __init__(self): self.comEnabled = True
    def OnCOMCallsDisabledEvent(self, reason): self.comEnabled = False
    def OnCOMCallsEnabledEvent(self): self.comEnabled = True

# The first line works in the exe, the second doesn't.
itunes = win32com.client.Dispatch("iTunes.Application")
#itunes = win32com.client.DispatchWithEvents("iTunes.Application", ITunesEvents)

lib = getattr(itunes, "LibraryPlaylist")
src = getattr(lib, "Source")
playlists = getattr(src, "Playlists")

print "Found %i playlists." % getattr(playlists, "Count")

を使用するDispatchと、プログラムは正しくコンパイルおよび実行されます。を使用DispatchWithEventsすると、コマンドラインから呼び出されたときにプログラムは正常に実行されますが、exeを実行すると次のエラーが発生します。

Traceback (most recent call last):
File "sandbox.py", line 16, in <module>
  itunes = win32com.client.DispatchWithEvents("iTunes.Application", ITunesEvents)
File "win32com\client\__init__.pyc", line 252, in DispatchWithEvents
File "win32com\client\gencache.pyc", line 520, in EnsureModule
File "win32com\client\gencache.pyc", line 287, in MakeModuleForTypelib
File "win32com\client\makepy.pyc", line 259, in GenerateFromTypeLibSpec
File "win32com\client\gencache.pyc", line 141, in GetGeneratePath
IOError: [Errno 2] No such file or directory: '[distDir]\\library.zip\\win32com\\gen_py\\__init__.py'

PyInstallerを使用してみましたが、同様のエラーが発生します。

File "<string>", line 16, in <module>
File "[outDir]/win32com.client", line 252, in DispatchWithEvents
File "[outDir]/win32com.client.gencache", line 520, in EnsureModule
File "[outDir]/win32com.client.gencache", line 287, in MakeModuleForTypelib
File "[outDir]/win32com.client.makepy", line 286, in GenerateFromTypeLibSpec
File "[outDir]/win32com.client.gencache", line 550, in AddModuleToCache
File "[outDir]/win32com.client.gencache", line 629, in _GetModule
File "[pyinstallerDir]\iu.py", line 455, in importHook
    raise ImportError, "No module named %s" % fqname
ImportError: No module named win32com.gen_py.9E93C96F-CF0D-43F6-8BA8-B807A3370712x0x1x13

ファイルにtypelibを手動で追加できることはわかっていsetup.pyますが、再コンパイルせずに異なるバージョンのiTunesを搭載したコンピューターでコードを実行したいので、動的に作成したいと思います。setup / specでこれを行う方法がない場合、イベントをロードする別の方法があるのではないでしょうか。ありがとう。


添加:

Ryanのおかげで、生成されたpyファイルを取得でき、少し掘り下げた後、次のことがわかりました。

生成されたpyファイル(からmakepy.py)を取得し、名前を。のように変更しcominterface.pyます。次に、イベントハンドラーを使用してCOMオブジェクトを実際に作成するには、次の手順を実行する必要があります。

import cominterface
from types import ClassType
from win32com.client import EventsProxy, _event_setattr_

class ItunesEvents:
    '''iTunes events class. See cominterface for details.'''
    def OnPlayerPlayEvent(self, t):print "Playing..."
    def OnPlayerStopEvent(self, t): print "Stopping..."

itunes = cominterface.iTunesApp()
rClass = ClassType("COMEventClass", (itunes.__class__, itunes.default_source, ItunesEvents), {'__setattr__': _event_setattr_})
instance = rClass(itunes._oleobj_)
itunes.default_source.__init__(instance, instance)
#ItunesEvents.__init__(instance) #Uncomment this line if your events class has __init__.
itunes = EventsProxy(instance)

その後、あなたはあなたのビジネスに取り掛かることができます。

4

3 に答える 3

2

まったく同じエラーが発生していました。このリンクは私を正しい方向に導きました-> http://www.py2exe.org/index.cgi/UsingEnsureDispatch しかしそれはそれを述べています:NBあなたはpython ... \win32com.client.gen_pydirがそうではないことを確認しなければなりません%temp%でキャッシュを作成できるようにするために存在しますが、これは少し混乱しました。私にとってそれを解決したのは、「C:\ Python26 \ Lib \ site-packages \ win32com \ gen_py」の名前を「C:\ Python26 \ Lib \ site-packages \ win32com \ gen_pybak」に変更することでした(py2exeを実行している場合)

于 2012-01-23T07:22:12.430 に答える
1

これが公式の方法です。

(編集:上記のリンクから逐語的な例をコピー)

import win32com.client
if win32com.client.gencache.is_readonly == True:

    #allow gencache to create the cached wrapper objects
    win32com.client.gencache.is_readonly = False

    # under p2exe the call in gencache to __init__() does not happen
    # so we use Rebuild() to force the creation of the gen_py folder
    win32com.client.gencache.Rebuild()

    # NB You must ensure that the python...\win32com.client.gen_py dir does not exist
    # to allow creation of the cache in %temp%

# Use SAPI speech through IDispatch
from win32com.client.gencache import EnsureDispatch
from win32com.client import constants
voice = EnsureDispatch("Sapi.SpVoice", bForDemand=0)
voice.Speak( "Hello World.", constants.SVSFlagsAsync )
于 2011-10-08T16:25:26.447 に答える
-1

キャッシュに依存する代わりに、ローカルキャッシュディレクトリに移動し、生成されたファイルをローカルプロジェクトファイルにコピーして、ITunesInterface.pyのような名前を付け、それを明示的に呼び出すことをお勧めします。これにより、py2exeがコンパイル済みアプリにプルします。

于 2010-04-26T14:45:44.223 に答える