Why is the exe produced by PyInstaller failing this import? - python

I am having trouble with PyInstaller. I am trying to build the main file (Logical.py -> Logical.exe). When I run the file as a python script it runs fine (terminal: python Logical.py examples/led.lgc). When I run the exe built by PyInstaller (terminal: ./Logical examples/led.lgc), I get this error message:
Traceback (most recent call last):
File "Logical.py", line 2, in <module>
from pynput import 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 76, in backend
ImportError
[10688] Failed to execute script Logical
It appears to be upset about the pynput import, though I have no idea why. The imports of my source are below. loading and ui are both in the project directory and simpleANSI's source code is listed at the bottom of this post.
import sys, time, os, ctypes
from pynput import keyboard
from loading.loading import loadElement
from ui import vec2, widget, ansiManager
import simpleANSI as ANSI
import pdb
I run both of these repositories so I can guarantee that they will not change until I can get this fixed.
Full project source code - https://github.com/AwesomeCronk/Logical
Main file - https://github.com/AwesomeCronk/Logical/blob/master/Logical.py.
simpleANSI source code - https://github.com/AwesomeCronk/simpleANSI
I am using Python 3.9.5 on Windows 10 x64, PyInstaller version 4.3, pynput version 1.7.3.
EDIT: I dug through the Python lib files and found the destination of the traceback. This is the offending function in ...\python\3.9.5\Lib\site-packages\pynput\_util\__init__.py:
def backend(package):
"""Returns the backend module for a package.
:param str package: The package for which to load a backend.
"""
backend_name = os.environ.get(
'PYNPUT_BACKEND_{}'.format(package.rsplit('.')[-1].upper()),
os.environ.get('PYNPUT_BACKEND', None))
if backend_name:
modules = [backend_name]
elif sys.platform == 'darwin':
modules = ['darwin']
elif sys.platform == 'win32':
modules = ['win32']
else:
modules = ['xorg']
errors = []
resolutions = []
for module in modules:
try:
return importlib.import_module('._' + module, package)
except ImportError as e:
errors.append(e)
if module in RESOLUTIONS:
resolutions.append(RESOLUTIONS[module])
raise ImportError('this platform is not supported: {}'.format( # AwesomeCronk: This is the offending line
'; '.join(str(e) for e in errors)) + ('\n\n'
'Try one of the following resolutions:\n\n'
+ '\n\n'.join(
' * {}'.format(s)
for s in resolutions))
if resolutions else '')

I tried adding --hidden-import "pynput.keyboard._win32" --hidden-import "pynput.mouse._win32" to the PyInstaller args to no avail. I wound up rolling back pynput to version 1.6.8.

Related

attempting to read pdf results in "OSError: Ghostscript is not installed" but it is

Trying to run a basic script:
import camelot
tables = camelot.read_pdf('foo.pdf')
results in:
Traceback (most recent call last):
File "C:\Users\username\Documents\GitHub\Scott\test_files\pdf_extract.py", line 2, in <module>
tables = camelot.read_pdf('foo.pdf')
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\camelot\io.py", line 113, in read_pdf
tables = p.parse(
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\camelot\handlers.py", line 176, in parse
t = parser.extract_tables(
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\camelot\parsers\lattice.py", line 421, in extract_tables
self.backend.convert(self.filename, self.imagename)
File "C:\Users\username\AppData\Local\Programs\Python\Python39\lib\site-packages\camelot\backends\ghostscript_backend.py", line 30, in convert
raise OSError(
OSError: Ghostscript is not installed. You can install it using the instructions here: https://camelot-py.readthedocs.io/en/master/user/install-deps.html
[Finished in 1.3s]
Running Windows 10 OS, Python 3.9.2. script is being run using sublime text.
I've installed both 32 and 64 bit versions of Ghostscript (gs9.55), added path statements to both bin and lib directories and checked the dll is registered in the registry. No luck.
Funny thing: when I run the commands line by line from the python command line, it works!
Running:
>>> import ctypes
>>> from ctypes.util import find_library
>>> find_library("".join(("gsdll", str(ctypes.sizeof(ctypes.c_voidp) * 8), ".dll")))
Produces - 'C:\Program Files\gs\gs9.55.0\bin\gsdll64.dll'
so the library is there.
I just discovered if I comment out this part in ghostscript_backend.py:
def convert(self, pdf_path, png_path, resolution=300):
# if not self.installed():
# raise OSError(
# "Ghostscript is not installed. You can install it using the instructions"
# " here: https://camelot-py.readthedocs.io/en/master/user/install-deps.html"
# )
The script works and then I can move on to getting tables and such. Is there a newer version of the ghostscript_backend.py I should be using?

Pyinstaller package not found even after a explicit hidden import (gino-starlette)

I've been trying to package an application which requires gino-starlette. I've tried including the requirement as a hidden import with command
pyinstaller --hidden-import gino_starlette --hidden-import gino.ext --noconfirm windows_runner.py --clean
but to no avail, the logs read that the gino_starlette was found, but running the executable gives the same error.
Log while running the executable:
Traceback (most recent call last):
File "gino\ext\__init__.py", line 72, in find_spec
ImportError: Cannot import gino.ext.starlette - is gino-starlette a valid extension and installed?
Piece of code which throws the specific error (this resides in gino.ext, which loads all extensions needed by gino):
def find_spec(self, fullname, path, target=None):
target = self._redirects.get(fullname)
if target:
mod = sys.modules.get(target)
if mod is None:
spec = find_spec(target)
spec.loader = _GinoExtensionCompatProxyLoader(fullname, spec.loader)
return spec
else:
return ModuleSpec(fullname, _GinoExtensionCompatNoopLoader(mod))
elif fullname.startswith(__name__):
raise ImportError(
"Cannot import {} - is gino-{} a valid extension and installed?".format(
fullname, fullname[len(__name__) + 1 :]
)
)
(I included the piece of code as https://pyinstaller.readthedocs.io/en/stable/when-things-go-wrong.html#listing-hidden-imports says that I should include it in hidden-import,which I did)
How should I proceed with debugging the problem?

Pyinstaller having issues with import of librairies

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.

"Failed to execute script " pyinstaller

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.

program wont work after being compiled with py2exe

I wrote a program it works fine without any errors when I run it from the python interpreter, I then used py2exe to turn it in to an .exe but when I run it it does'nt work anymore... I get this error:
Traceback (most recent call last):
File "pass.py", line 1, in <module>
File "dropbox\__init__.pyc", line 3, in <module>
File "dropbox\client.pyc", line 22, in <module>
File "dropbox\rest.pyc", line 26, in <module>
File "pkg_resources.pyc", line 950, in resource_filename
File "pkg_resources.pyc", line 1638, in get_resource_filename
NotImplementedError: resource_filename() only supported for .egg, not .zip
Am I supposed to do something when using py2exe when I have downloaded modules imported into the program?
these are the modules imported :
import dropbox
import os
import getpass
from time import sleep
please help !
I fixed this problem using the solution found here
basically you modify ~line 26 in rest.py found in the dropbox library
to this:
try:
TRUSTED_CERT_FILE = pkg_resources.resource_filename(__name__, 'trusted-certs.crt')
except:
# if current module is frozen, use library.zip path for trusted-certs.crt path
# trusted-certs.crt must exist in same directory as library.zip
if hasattr(sys,'frozen'):
resource_name = sys.prefix
resource_name.strip('/')
resource_name += '/trusted-certs.crt'
TRUSTED_CERT_FILE = resource_name
else:
raise
and then place the trusted-certs.crt file also found in the dropbox library in the same folder as your executable

Categories

Resources