11

I'm trying to convert a simple Python script into a Windows executable. My setup.py script is:

from distutils.core import setup
import py2exe

setup(
    name = "Simple Script",
    options = {
        "py2exe": {
            "dll_excludes" : ["libmmd.dll","libifcoremd.dll","libiomp5md.dll","libzmq.dll"]
        }
    },
    console=['simple_script.py']
)

I have added the dll_excludes as each one of them caused a failure. Now I've hit a failure that I can't simply exlude. This is the error trace:

Traceback (most recent call last):
  File "setup.py", line 12, in <module>
    console=['rules signed.py']
  File "C:\Anaconda\lib\distutils\core.py", line 152, in setup
    dist.run_commands()
  File "C:\Anaconda\lib\distutils\dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "C:\Anaconda\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Anaconda\lib\site-packages\py2exe\build_exe.py", line 243, in run
    self._run()
  File "C:\Anaconda\lib\site-packages\py2exe\build_exe.py", line 306, in _run
    self.plat_finalize(mf.modules, py_files, extensions, dlls)
  File "C:\Anaconda\lib\site-packages\py2exe\build_exe.py", line 1157, in plat_finalize
    import pythoncom
  File "C:\Anaconda\lib\site-packages\pythoncom.py", line 2, in <module>
    import pywintypes
  File "C:\Anaconda\lib\site-packages\win32\lib\pywintypes.py", line 124, in <module>
    __import_pywin32_system_module__("pywintypes", globals())
  File "C:\Anaconda\lib\site-packages\win32\lib\pywintypes.py", line 98, in __import_pywin32_system_module__
    raise ImportError("No system module '%s' (%s)" % (modname, filename))
ImportError: No system module 'pywintypes' (pywintypes27.dll)

I've installed pywin32 and tried excluding "pywintypes27.dll", "pywintypes", "pywin", "pywin.debugger" in my setup options, with no success. Also tried applying all other solutions I found on SO, such as moving "pythoncom27.dll", "pythoncomloader27.dll", and "pywintypes27.dll" to a top level directory.

Nothing has eliminated the "ImportError: No system module 'pywintypes' (pywintypes27.dll)" problem

4

4 回答 4

20

我最近安装了 Anaconda,部分原因是我需要 win32com 包,并且不想排除 dll 文件。但是,对我来说同样的问题。

解决方案是复制 DLL 文件:
pywintypes27.dll
pythoncom27.dll
位于:
C:\Anaconda\Lib\site-packages\win32

C:\Anaconda\Lib\site-packages\win32\lib

因为查找这些文件的函数会在那里查找但不在上面的目录中。源文件 pywintypes.py 中的许多注释表明存在问题,可能是由于不同的安装过程。我在此处的 Anaconda 问题跟踪器上发布了一个问题。

于 2013-11-16T11:33:49.873 回答
1

我遇到了 py2exe 无法找到 pywintypes27.dll 的另一个问题 - 它无法在 build_exe.isSystemDLL 中找到文件。解决方案是在路径中添加 DLL 的位置(至少 hack 是这样做的):

import site
for site_path in site.getsitepackages():
    pywin32_path = os.path.join(site_path, "pywin32_system32")
    if os.path.isdir(pywin32_path):
        os.environ["PATH"] = os.environ["PATH"] + ";" + pywin32_path
于 2015-04-08T16:44:12.593 回答
1

这是我日常用于将控制台 python 应用程序打包为 exe 的代码片段。它工作正常。

from distutils.core import setup
import py2exe
from glob import glob

data_files = [("Microsoft.VC90.CRT",
              glob(r'C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\*.*')), 
             ... other required files]
py2exe_options={"py2exe":{"includes":[some_thing_need_to_included], 
                          "dll_excludes":[some_thing_need_to_exclude]}}
setup(data_files=data_files, 
      options=py2exe_options,
      console=['aim_python_script.py'])

您应该检查“simple_script.py”的内容。它是否引用了一些特殊的第三方库?

于 2013-10-21T06:26:46.817 回答
1

这里有一个类似的问题:https ://github.com/ContinuumIO/anaconda-issues/issues/37 。我看到你使用 Anaconda,我认为这是 anaconda 和 python 解释器的问题。

本质上,使用 IPython 解释器时不存在该问题!例如尝试:

C:\...\User> python
>>>import pythoncom
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\Anaconda3\lib\site-packages\pythoncom.py", line 2, in <module>
    import pywintypes
  File "C:\Program Files\Anaconda3\lib\site-packages\win3\lib\pywintypes.py", line 124, in <module>
    __import_pywin32_system_module__("pywintypes", globals())
  File "C:\Program Files\Anaconda3\lib\site-packages\win32\lib\pywintypes.py", line 98, in __import_pywin32_system_module__
raise ImportError("No system module '%s' (%s)" % (modname, filename))
ImportError: No system module 'pywintypes' (pywintypes34.dll)

另一方面,尝试

C:\...\User> ipython
In [1]: import pythoncom

In [2]: pythoncom
Out[2]: <module 'pythoncom' from 'C:\\Program Files\\Anaconda3\\lib\\site-packages\\win32\\pythoncom34.dll'>

使用 IPython 没问题!

儿子,直到这个问题得到解决,你可以使用 IPython 解释器来运行你麻烦的 .py 文件,例如:

C:\...\User> ipython setup.py

这应该有效。您应该通过 a 将要从命令传递给脚本的参数分开--,否则 IPython 可能会尝试解析它,例如使用:

C:\...\User> ipython setup.py -- arg1 arg2

在解决此问题之前,请尝试此方法。

于 2015-06-22T17:38:11.027 回答