当我使用原始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'],
)
尝试过的版本:
摘要:如何以正确的方式使用带有py2exe的wmi模块?
答案 0 :(得分:0)
由于我确实清楚地重新安装了python 2.7(当然还有必要的模块),所以一切都在我的Windows 10上运行,但是在winpe 10上仍然没有!
我必须在winpe图像中添加wmi包,之前的winpe版本默认包含它!现在它也适用于winpe。
(我无法理解。如果我知道的话,wmi是winpe上最常用的工具之一!)
使用这两行命令:
Dism /Add-Package /Image:"C:\WinPE_amd64\mount" /PackagePath:"C:\Program Files\Windows Kits\8.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\8.1\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64\WinPE_OCs\en-us\WinPE-WMI_en-us.cab"