可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
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.
回答2:
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?
回答3:
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
回答4:
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.