程序不能在 py2exe 之外运行
Program doesn't work beyond py2exe
当我使用原始 py 文件时,我的代码运行良好。但是当我用 py2exe 编译时,它会抛出一个 AttributeError:
File "test.py", line 1, in <module>
import wmi
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
File "C:\Python34\lib\site-packages\zipextimporter.py", line 86, in load_module
return zipimport.zipimporter.load_module(self, fullname)
File "C:\Python34\lib\wmi.py", line 88, in <module>
from win32com.client import GetObject, Dispatch
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
File "C:\Python34\lib\site-packages\zipextimporter.py", line 86, in load_module
return zipimport.zipimporter.load_module(self, fullname)
File "C:\Python34\lib\site-packages\win32com\__init__.py", line 6, in <module>
import pythoncom
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
File "C:\Python34\lib\site-packages\zipextimporter.py", line 86, in load_module
return zipimport.zipimporter.load_module(self, fullname)
File "C:\Python34\lib\site-packages\pythoncom.py", line 3, in <module>
pywintypes.__import_pywin32_system_module__("pythoncom", globals())
AttributeError: 'module' object has no attribute '__import_pywin32_system_module__'
当我将我的代码反向移植到 2.7 时,它会运行良好,但在 winpe10(Windows 预安装环境,非常精简版的 win10)上会出现同样的错误。
在 winpe 5(它是 win8.1 的精简版)上也可以工作。
我的示例代码:
import wmi
def getWmiData(wmiProperty, wmiClass, wmiNamespace='cimv2'):
"""Return array of strings."""
wmiValues = []
wmiCursore = wmi.GetObject('winmgmts:\root\' + wmiNamespace)
wmiQuery = 'SELECT ' + wmiProperty + ' FROM ' + wmiClass
for item in wmiCursore.ExecQuery(wmiQuery):
wmiValues.append(str(item.__getattr__(wmiProperty)))
return wmiValues
print(getWmiData('SystemSKU', 'MS_systeminformation', 'wmi')[0])
py2exe 基本设置:
from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
setup(
options = {'py2exe': {'compressed': True},},
console = ['test.py'],
)
尝试过的版本:
- python 2.7.11, 2.7.12, 3.4.4
- py2xe: 0.6.9, 0.9.2.2
- pywin32: 219, 220
- wmi: 1.4.9
总结:如何以正确的方式将 wmi 模块与 py2exe 一起使用?
因为我确实重新安装了 python 2.7(当然还有必要的模块),所以在我的 windows 10 上一切正常,但是在 winpe 10 上仍然没有!
我不得不将wmi包添加到winpe镜像中,以前的winpe版本默认包含它!现在它也适用于 winpe。
(我看不懂,如果我知道的话,wmi是winpe上使用最多的工具之一!)
用这两行命令:
Dism /Add-Package /Image:"C:\WinPE_amd64\mount" /PackagePath:"C:\Program Files\Windows Kits.1\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64\WinPE_OCs\WinPE-WMI.cab"
Dism /Add-Package /Image:"C:\WinPE_amd64\mount" /PackagePath:"C:\Program Files\Windows Kits.1\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64\WinPE_OCs\en-us\WinPE-WMI_en-us.cab"
当我使用原始 py 文件时,我的代码运行良好。但是当我用 py2exe 编译时,它会抛出一个 AttributeError:
File "test.py", line 1, in <module>
import wmi
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
File "C:\Python34\lib\site-packages\zipextimporter.py", line 86, in load_module
return zipimport.zipimporter.load_module(self, fullname)
File "C:\Python34\lib\wmi.py", line 88, in <module>
from win32com.client import GetObject, Dispatch
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
File "C:\Python34\lib\site-packages\zipextimporter.py", line 86, in load_module
return zipimport.zipimporter.load_module(self, fullname)
File "C:\Python34\lib\site-packages\win32com\__init__.py", line 6, in <module>
import pythoncom
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
File "C:\Python34\lib\site-packages\zipextimporter.py", line 86, in load_module
return zipimport.zipimporter.load_module(self, fullname)
File "C:\Python34\lib\site-packages\pythoncom.py", line 3, in <module>
pywintypes.__import_pywin32_system_module__("pythoncom", globals())
AttributeError: 'module' object has no attribute '__import_pywin32_system_module__'
当我将我的代码反向移植到 2.7 时,它会运行良好,但在 winpe10(Windows 预安装环境,非常精简版的 win10)上会出现同样的错误。
在 winpe 5(它是 win8.1 的精简版)上也可以工作。
我的示例代码:
import wmi
def getWmiData(wmiProperty, wmiClass, wmiNamespace='cimv2'):
"""Return array of strings."""
wmiValues = []
wmiCursore = wmi.GetObject('winmgmts:\root\' + wmiNamespace)
wmiQuery = 'SELECT ' + wmiProperty + ' FROM ' + wmiClass
for item in wmiCursore.ExecQuery(wmiQuery):
wmiValues.append(str(item.__getattr__(wmiProperty)))
return wmiValues
print(getWmiData('SystemSKU', 'MS_systeminformation', 'wmi')[0])
py2exe 基本设置:
from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')
setup(
options = {'py2exe': {'compressed': True},},
console = ['test.py'],
)
尝试过的版本:
- python 2.7.11, 2.7.12, 3.4.4
- py2xe: 0.6.9, 0.9.2.2
- pywin32: 219, 220
- wmi: 1.4.9
总结:如何以正确的方式将 wmi 模块与 py2exe 一起使用?
因为我确实重新安装了 python 2.7(当然还有必要的模块),所以在我的 windows 10 上一切正常,但是在 winpe 10 上仍然没有!
我不得不将wmi包添加到winpe镜像中,以前的winpe版本默认包含它!现在它也适用于 winpe。
(我看不懂,如果我知道的话,wmi是winpe上使用最多的工具之一!)
用这两行命令:
Dism /Add-Package /Image:"C:\WinPE_amd64\mount" /PackagePath:"C:\Program Files\Windows Kits.1\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64\WinPE_OCs\WinPE-WMI.cab"
Dism /Add-Package /Image:"C:\WinPE_amd64\mount" /PackagePath:"C:\Program Files\Windows Kits.1\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64\WinPE_OCs\en-us\WinPE-WMI_en-us.cab"