Döme Döme - 3 months ago 19
Python Question

Program doesn't work beyond py2exe

My code is worked well, when I used the raw py file. But when I compiled with py2exe, it will drop an 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__'


When I backported my code to 2.7, It will worked well, but on on winpe10 (Windows Preinstallation Environment, very thin version of win10) drop same error.
On winpe 5 (it is thin version of win8.1) work as well.

My example code:

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


Basic py2exe settings:

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

setup(
options = {'py2exe': {'compressed': True},},
console = ['test.py'],
)


Tried versions:


  • 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



Summary: How can I use wmi module with py2exe on the right way?

Answer

Since I did clearly reinstall python 2.7 (and of course the necessary modules), everything works on my windows 10, however on the winpe 10 still did not!

I had to add wmi packages to the winpe image, the previous winpe version contains it by default! Now It works on winpe as well.
(I can't understand it. If I know well, wmi is one of the most used tool on winpe!)

With this two lines commands:

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"
Comments