py2exe - No system module 'pywintypes'

牧云@^-^@ 提交于 2019-12-04 01:19:58

I recently installed Anaconda, partly because I need the win32com package, and don't want to exclude dll files. However, same problem for me.

Solution was to copy the DLL files:
pywintypes27.dll
pythoncom27.dll
sitting in:
C:\Anaconda\Lib\site-packages\win32
to
C:\Anaconda\Lib\site-packages\win32\lib

Because the function looking for those files look there but not in the directory above. Lots of comments in the source file pywintypes.py reveals there has been issues with this, probably due to different install procedures. I have posted an issue on the Anaconda issue tracker here.

Here is a code snippet of my daily use to package console python app to exe. It's works fine.

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'])

You should check the content of your 'simple_script.py'. Does it reference some special third party library?

I had a different problem with py2exe failing to find pywintypes27.dll - it was failing to find the file inside build_exe.isSystemDLL. The solution is to add the location of the DLLs in the path (at least the hack is to do so):

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

There is a similar issue here: https://github.com/ContinuumIO/anaconda-issues/issues/37. I see you use Anaconda, and I think this is an issue with anaconda and the python interpreter.

Essentially, the issue is not present when using the IPython interpreter instead! Try for instance:

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)

On the other hand, try

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'>

No problem when using IPython!

Son until this gets fixed, you can run your troublesome .py files using the IPython interpreter instead, eg:

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

and that should work. You should seperate arguments you want to pass to your script from the command by a --, otherwise IPython might attempt to parse it, eg use:

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

Until this is fixed, try this method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!