我有一个使用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

我知道我可以在我的 setup.py 文件,但是我想在具有不同版本的iTunes的计算机上运行代码而无需重新编译,因此我更喜欢动态创建它。如果无法使用设置/规格来执行此操作,也许还有另一种加载事件的方法?谢谢。


添加:

多亏了Ryan,我发现我可以使用生成的PY文件,经过一点挖掘,就可以提出以下内容。

获取生成的PY文件(从 makepy.py)并将其重命名为 cominterface.py. 。然后,您需要执行以下操作,以实际创建COM对象,并使用事件处理程序创建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)

然后,您可以开展业务。

有帮助吗?

解决方案

我建议您建议进入本地缓存目录,将生成的文件复制到您的本地项目文件中,并将其命名为iTunesInterface.py,并明确拨打该文件。这将使PY2EXE将其拉入您的编译应用程序中。

其他提示

我经历了完全相同的错误。这个链接使我朝着正确的方向 - >http://www.py2exe.org/index.cgi/usingensuredispatch但是,它提到:nb您必须确保不存在python ... win32com.client.gen_py dir以允许在%temp%中创建缓存,这有点令人困惑。为我解决的问题是重命名“ C: Python26 lib lib site-packages win32com gen_py”到“ c: python26 lib lib lib site-packages win32com win32com gen_pybak”(运行PY2EXE时)

是这样做的官方方法。

(编辑:上面该链接的逐字复制示例)

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 )
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top