I was working with python (which I rarely do) for a simple program that macros some functionalities with certain keyboard inputs. I decided to export the script to a .exe using pyinstaller so I go ahead and run command: pyinstaller --onefile code.py and it generates the code.exe as expected. Only for my .exe to crash instantly. I got a view of the error by running it with cmd and got this
Traceback (most recent call last):
File "code.py", line 1, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "pynput\__init__.py", line 40, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "pynput\keyboard\__init__.py", line 31, in <module>
File "pynput\_util\__init__.py", line 76, in backend
ImportError
[1284] Failed to execute script 'code' due to unhandled exception!
I'm certain there is an explanation to this so here are my imports:
from pynput import keyboard
from pynput.keyboard import Key
from pywinauto import application
from pywinauto.findwindows
import WindowAmbiguousError, WindowNotFoundError
import os
I couldn't seem to find any similar problems on here. Thank you in advance for the help.
In your .spec file, you should add all of your modules as "hidden imports", this usually fixes all module not found errors.
hiddenimport=["pynput","pywinauto"]
Then run pyinstaller with the spec file
pyinstaller my.spec
I'd also ensure you're using a virtual environment during this entire process.
Related
So after I compiled the program I wrote using pyinstaller in one-file mode, The program doesn't seem to function at all.
When I try to execute it, I receive the following Exception in a Window:
Traceback (most recent call last):
File "main.py", line 6, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "pystray\__init__.py", line 48, in <module>
File "pystray\__init__.py", line 44, in backend
ImportError: this platform is not supported: No module named 'pystray._win32'
What could be the issue here? I mean how do I solve this problem?
I'm currently running:
Python 3.9
Windows 10 (the latest version)
Visual Studio Code
Try using the git bash or command prompt. I don't think PyInstaller works with the VSC terminal.
I have a python code that I wanted to turn into an exe.
I used pyinstaller with : pyinstaller --onefile -w script.py
After it finished making the exe file I double clicked the file but I got "Failed to execute script".
I also tried running it from the cmd but it gives the same fatal error.
Stuff to add:
The code imports a couple of files packages including a python code I made, as well as it makes files referenced to it's location.
Is there something I'm doing wrong?
the script has those imports:
import socket
import os
from PIL import ImageGrab
import cv2
import time
import json
import myFile
I ran the code under cmd and it gives this error:
File "script.py", line 3, in <module>
from PIL import ImageGrab
ModuleNotFoundError: No module named 'PIL'
Might be unrelated but now that I tried to do pyinstaller --onefile -w client.py.
After I ran it windows defender found this inside :
after running it in the terminal in pycharm with :
pyinstaller --onefile --hidden-import=PIL --hidden-import=Pillow --hidden-import=pynput client.py
I get this error (note that i moved it from the dist directory to the main one):
Traceback (most recent call last):
File "client.py", line 7, in <module>
import myFile
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "myFile.py", line 1, in <module>
from pynput import mouse, keyboard
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "pynput\__init__.py", line 40, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
File "pynput\keyboard\__init__.py", line 31, in <module>
File "pynput\_util\__init__.py", line 82, in backend
ImportError
[13364] Failed to execute script client
This may be due to pyinstaller not being able to properly find your dependencies and skipping some packages.
To fix any error like ModuleNotFoundError: No module named 'PIL' just add it as a hidden import:
pyinstaller --onefile --hidden-import=PIL -w script.py
For the second error this is a known issue with pyinstaller and pynput.
Find here some explanation.
The TLDR of it seems to be that you need to add --hidden-import=pynput.mouse._win32 --hidden-import=pynput.keyboard._win32 --hidden-import=pynput._util._win32 and any other sub-packages that give you errors.
i wrote this python script "mycv.py" in raspbian:
import cv2 as cv
and it debug and run currectly. then i use pyinstaller to make executable file.
but when run file in terminal. it has error:
Traceback (most recent call last):
File "mycv.py", line 1, in <module>
import cv2 as cv
File "/usr/local/lib/python3.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "cv2/__init__.py", line 89, in <module>
File "cv2/__init__.py", line 58, in bootstrap
File "cv2/__init__.py", line 56, in load_first_config
ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.
[28400] Failed to execute script mycv
Any help is appreciated
After lots of debugging, I found the following solution:
Get the path of OpenCV
import cv2
print(cv2.file) # /usr/local/lib/python3.6/dist-packages/cv2/python-3.6/cv2.so
Add this path while compiling through pyinstaller
pyinstaller main.py -n myApp --paths="/usr/local/lib/python3.6/dist-packages/cv2/python-3.6"
I hope this helps others also
I'm just sruggling with importing modules from nested packages in Python.
After execute command in project root directory:
$ nosetests
Unfortunatelly, i'm still getting logs like this:
======================================================================
ERROR: Failure: ImportError (No module named io_file)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/nose/loader.py", line 418, in loadTestsFromName
addr.filename, addr.module)
File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 47, in importFromPath
return self.importFromDir(dir_path, fqname)
File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 94, in importFromDir
mod = load_module(part_fqname, fh, filename, desc)
File "/home/user/dev/ease-ci/easeci-core/tests/lib/io/test_io_facade.py", line 4, in <module>
from lib.io.io_file import file_load, File, file_exist, file_save, file_delete, file_change
ImportError: No module named io_file
And more and more errors like that.
Can someone tell me something help me to resolve my issue? Thanks.
Pycharm put me in error, because if I run test by green arrow, everything is ok.
I'm guessing that you are trying to install a library without pip installed because you used pycharm in a weird way.
Try adding a path to the library which you have to download and then run the import within the code. Cheers!
import tensorflow as tf
https://www.jetbrains.com/help/pycharm/absolute-path-variables.html
I was trying to execute the following command on windows 10 machine using python 3.5.4,
from pywinauto.application import Application
Following is the error which i was receiving
Traceback (most recent call last):
File "C:/Users/bizact/Desktop/PSAV/test.py", line 1, in <module>
from pywinauto.application import Application
File "C:\Program Files\Python35\lib\site-packages\pywinauto\__init__.py", line 72, in <module>
from . import findwindows
File "C:\Program Files\Python35\lib\site-packages\pywinauto\findwindows.py", line 42, in <module>
from . import controls
File "C:\Program Files\Python35\lib\site-packages\pywinauto\controls\__init__.py", line 36, in <module>
from . import uiawrapper # register "uia" back-end (at the end of uiawrapper module)
File "C:\Program Files\Python35\lib\site-packages\pywinauto\controls\uiawrapper.py", line 46, in <module>
from ..uia_defines import IUIA
File "C:\Program Files\Python35\lib\site-packages\pywinauto\uia_defines.py", line 35, in <module>
import comtypes.client
File "C:\Program Files\Python35\lib\site-packages\comtypes\client\__init__.py", line 33, in <module>
gen_dir = _find_gen_dir()
File "C:\Program Files\Python35\lib\site-packages\comtypes\client\_code_cache.py", line 71, in _find_gen_dir
result = os.path.abspath(gen_path[-1])
IndexError: list index out of range
Is there a way i can fix this ?
Try running the script with administrative privileges. Just by looking at the traceback it looks like some cached files that pywinauto import tried to write but later on it wasn't found, and generally the script can't write the file due to permissions. If the above don't work, try installing python somewhere else (C:/Python with full privileges in order to isolate the issue)
This is comtypes issue. It will be fixed soon (EDIT: fixed in comtypes==1.1.7). You can downgrade to one of previous versions: pip install comtypes==1.1.2. Or run script as Administrator as Rodolfo suggested (disabling UAC may not help, need to say explicitly to "run as Administrator" by popup menu).