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?
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"
Related
After I converted my python project with NUITKA and I started running the following message appeared:
Owlready2 * Warning: optimized Cython parser module 'owlready2_optimized' is not available, defaulting to slower Python implementation
Traceback (most recent call last):
File "/user/try.dist/try.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "/user/try.dist/owlready2/__init__.py", line 35, in <module owlready2>
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "/user/try.dist/owlready2/reasoning.py", line 41, in <module owlready2.reasoning>
FileNotFoundError: [Errno 2] No such file or directory: '/user/try.dist/owlready2/pellet'
Indicating that after I compiled the program some files were not found.
The code that I run was python3 -m nuitka --follow-imports --enable-plugin=numpy --onefile try.py
and the code that I compiled was just simple this:
from owlready2 import *
onto = get_ontology("file:///user/ontologies/try.owl").load()
with onto:
types.new_class("Drug", (Thing,))
onto.save()
So simple there was something wrong with the library.
If anyone knows what to do it will be helpful.
Thanks in advance.
I have changed the location of the file for privacy
I am trying to package up a script with pyinstaller that uses the skyfield module/API, and it builds, but when I run it, it dies saying it can't find nutation.npz (a data file included in the nutationlib.py _arrays = load_bundled_npy('nutation.npz') ).
I have tried using --onefile, not using it. --add-data and --add-binary don't like it. --hidden-imports doesn't help. --debug=imports wasn't too useful, but the attached log was from --debug=all compilation.
Any thoughts on how to force pyinstaller to include nutation.npz into the built .exe so its there when its extracted/run?
import skyfield.nutationlib # PyInstaller PYZ
Traceback (most recent call last):
File "satvis.py", line 9, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "c:\users\fox\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\skyfield\api.py", line 11, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "c:\users\fox\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\skyfield\constellationlib.py", line 29, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "c:\users\fox\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\skyfield\timelib.py", line 14, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "c:\users\fox\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\skyfield\nutationlib.py", line 7, in <module>
File "site-packages\skyfield\functions.py", line 143, in load_bundled_npy
File "pkgutil.py", line 637, in get_data
File "c:\users\fox\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 471, in get_data
with open(path, 'rb') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\fox\\AppData\\Local\\Temp\\_MEI89762\\skyfield\\data\\nutation.npz'
[44412] Failed to execute script satvis
You need to add a directory with skyfield data files (npz, npy, etc).
For example, if skyfield located in the virtual environment folder, you need the following pyinstaller call:
pyinstaller --onefile --add-data 'venv/Lib/site-packages/skyfield/data;skyfield/data' yourapp.py
In Skyfield's code, it looks like it thinks of that file as belonging to the package "skyfield":
data = get_data('skyfield', 'data/{0}'.format(filename))
But in Skyfield's setup.py, that file belongs to the subpackage skyfield.data instead:
package_data = {
'skyfield': ['documentation/*.rst'],
'skyfield.data': ['Leap_Second.dat', 'deltat.data', 'deltat.preds',
'*.npy', '*.npz'],
'skyfield.tests': ['data/*'],
},
If you have the ability to edit your local copy of Skyfield, try changing the above-quoted line of code from skyfield/functions.py so that it says:
data = get_data('skyfield.data', '{0}'.format(filename))
There is some chance that the change will fix the problem with PyInstaller. If it does, I will make the change officially to Skyfield’s code!
I was able to build a working "onefile" of myprog.exe on Win10 with
python 3.7.9
pyinstaller 4.0
skyfield 1.29
by building a spec file using the command
pyinstaller -F myprog.py
then in the spec file, changing the line
datas=[],
to
datas=[ ('C:\\Users\\Administrator\\Anaconda3\\pkgs\\skyfield-1.28-pyh9f0ad1d_0\\site-packages\\*', 'skyfield')],
The build command I used was
pyinstaller -clean myprog.spec
I don't believe Anaconda had anything to do with it, that's just where my installation has the skyfield package data. I did build it in a standard Python virtual environment however.
I’m running into an issue getting pyInstaller to bundle a module that contains SWIG modules. I have followed each of the guidelines listed in the pyInstaller doc which details SWIG support.
I get the following error when running the exe:
# sphinxbase._ad_win32 not found in PYZ
Traceback (most recent call last):
File "StartListening.py", line 10, in <module>
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "c:\users\allen\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\sphinxbase\__init__.py", line 35, in <module>
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "c:\users\allen\appdata\local\programs\python\python36-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\sphinxbase\ad_win32.py", line 32, in <module>
File "site-packages\sphinxbase\ad_win32.py", line 31, in swig_import_helper
File "importlib\__init__.py", line 126, in import_module
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'sphinxbase._ad_win32'
However, sphinxbase._ad_win32 does exists as a pyd file. I’m running Windows 10; why is pyInstaller having trouble bundling this module?
The third-party package I’m ultimately trying to import is SpeechRecognition which depends on pocketsphinx which depends on sphinxbase which is the module that is having issues.
Here is the object reference returned by
importlib.import_module('sphinxbase._ad_win32')
in the SWIG-generated python wrapper if that is helpful to someone:
<module 'sphinxbase._ad_win32' from 'C:\\Users\\bob\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\sphinxbase\\_ad_win32.pyd'>
Python version: 3.6.5
Edit: After some more research, I tried adding the .pyd to the 'binaries' list in the .spec file as well as adding 'sphinxbase._ad_win32' to the 'hidden-imports' list and combinations there-between, but this did not work either. The binary is there, in the correct location, I can't figure out why the bundled app can't find it.
Running this code in normal IDLE is completely fine.
import OpenGL.GL as gl
However in VSCode, I get an import error.
No module named 'OpenGL.GL'; 'OpenGL' is not a package
File "C:\Users\Fluffy\Desktop\OpenGL.py", line 1, in <module>
import OpenGL.GL as gl
File "C:\users\fluffy\desktop\<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\users\fluffy\desktop\<frozen importlib._bootstrap_external>", line 678, in exec_module
File "C:\users\fluffy\desktop\<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "C:\users\fluffy\desktop\<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "C:\users\fluffy\desktop\<frozen importlib._bootstrap>", line 971, in _find_and_load
File "C:\users\fluffy\desktop\<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\users\fluffy\desktop\<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "C:\users\fluffy\desktop\<frozen importlib._bootstrap>", line 971, in _find_and_load
File "C:\Users\fluffy\Desktop\OpenGL.py", line 1, in <module>
import OpenGL.GL as gl
Furthermore, I uninstalled PyOpenGL and noticed that VSCode's intellisense found an existing OpenGL module within python:
link
I'm pretty sure it has something to do with VSCode's way of importing modules from python.
NOTE: I tried reinstalling the PyOpenGL in many different ways, the error still persists.
I made my py file executable using py2exe. My setup.py is as follows:
from distutils.core import setup
import py2exe
setup(windows=['main.py'])
When I tried to run main.exe, I get an error and was referred to main.log which reads:
Traceback (most recent call last):
File "main.py", line 20, in <module>
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\pandas\__init__.py", line 7, in <module>
from . import hashtable, tslib, lib
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 "<loader>", line 10, in <module>
File "<loader>", line 6, in __load
File "pandas\tslib.pyx", line 2839, in init pandas.tslib (pandas\tslib.c:79846)
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 "<loader>", line 10, in <module>
File "<loader>", line 6, in __load
File "pandas\algos.pyx", line 64, in init pandas.algos (pandas\algos.c:179610)
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 "<loader>", line 10, in <module>
File "<loader>", line 6, in __load
File "pandas\lib.pyx", line 1, in init pandas.lib (pandas\lib.c:77889)
AttributeError: 'module' object has no attribute '__pyx_capi__'
It looks like line 20 in my main.py file (import pandas as pd) triggered the problem. This type of error is referenced in a pull request here and they suggested adding a blank __init__.py file. I did so and got same result. I found a well commented post here (not directly dealing with py2exe) that suggests this might be due to mutual top-level imports or circular dependencies. This problem seems well known and has been mentioned at least twice on Stackoverflow in the past year (e.gs: here and here) but no clear solution has been given.
Perhaps we can get more clarity now with your feedback.
Python 3.4.2; py2exe 0.9.2.0
Was running into the same issue with pandas, but I got it working after updating to the most recent version.
It seems this error was recently addressed, and was resolved in Pandas 15.2
https://github.com/pydata/pandas/issues/8602
I was running in the same errors with Python 3.4.4.
My solution was to fix the import statement in C:\Pythob34\lib\site-packagespythoncom.py.
Original:
import pywintypes
pywintypes.import_pywin32_system_module("pythoncom", globals())
Changed To:
from pywintypes import import_pywin32_system_module
import_pywin32_system_module("pythoncom", globals())