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.
Related
I'm trying to use pyinstaller with a simple python program. Creating the windows .exe completes successfully but when I run the .exe program, I get the error
.\dist\myapp\myapp.exe
Traceback (most recent call last):
File "pac_cleaner\myapp.py", line 2, in <module>
import pyreadstat as pr
File "PyInstaller\loader\pyimod03_importers.py", line 495, in exec_module
File "pyreadstat\__init__.py", line 17, in <module>
File "pyreadstat\pyreadstat.pyx", line 1, in init pyreadstat.pyreadstat
ModuleNotFoundError: No module named 'pyreadstat._readstat_writer'
[11460] Failed to execute script 'myapp' due to unhandled exception!
The program itself is
import pyreadstat as pr
import pandas as pd
def hello():
print("Hello")
if __name__ == '__main__':
hello()
and I use pyinstaller as so
pyinstaller .\pac_cleaner\myapp.py --noconfirm --hidden-import=pandas --hidden-import=pyreadstat
any ideas?
Using the --collect-submodules works. I'm posting the answer from the official repo discussion
pyinstaller .\pac_cleaner\myapp.py --noconfirm --collect-submodules pyreadstat
I'm using Tkinter on MacOS and its libraries are a little broken, so there is an extension called tkmacosx that fixes many bugs.
All goes fine if I run the program from terminal with python3 file.py or from VSCode.
Then I tried to install it with pyinstaller, goes fine and when running the executable this is the output :
from tkmacosx import Button
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "tkmacosx/__init__.py", line 28, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "tkmacosx/variables/__init__.py", line 15, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "tkmacosx/variables/colorvar.py", line 18, in <module>
File "PyInstaller/loader/pyimod03_importers.py", line 546, in exec_module
File "tkmacosx/utils/__init__.py", line 17, in <module>
ModuleNotFoundError: No module named 'colour'
I checked and colour is a library inside/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (0.1.5) so I tried to add this library to all python versions because I thought that maybe one of them miss this package and adding it to all would fix :
ls /Library/Frameworks/Python.framework/Versions
output :
2.7 3.10 3.5 3.9 Current
So I added colour to each version running (not running on Current bcz it's not a version ?) :
python_version -m pip install colour
But I still have the same error. It's like if Python can't find the right module path when launching the executable but if I run it from the interpreter with python3 or from VSCode all works fine.
Consider I didn't set up an environment, I just created files, classes and solved imports. But I created this __init__.py file :
import os
import sys
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
Because it solved a previous import problem.
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.
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 use py2exe to compile python script. The python script runs without any error before compiling.
below is the setup.py
from distutils.core import setup
import py2exe, sys, os
import matplotlib
import numpy
from glob import glob
sys.argv.append('py2exe')
datafiles = [("Microsoft.VC90.CRT", glob(r'C:\Windows\WinSxS\amd64_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.1_none_99b61f5e8371c1d4\*.*'))]
datafiles.extend(matplotlib.get_py2exe_datafiles())
setup(windows=['Run.py'], data_files= datafiles, options={"py2exe": {"includes": ["matplotlib"]}})
In the run.py, I imported pandas. Pandas is used in a function as below
import pandas
def PossDist(Iterations, AssumptionFolder, ResultFolder, SampledResult):
SampledLoss = pandas.read_csv(SampledResult)
d= pandas.pivot_table(...)
The compile finished successfully without error.
When I run the compiled run.exe, I got error as below.
Traceback (most recent call last):
File "Run.py", line 6, in
File "xlwings__init__.pyc", line 34, in
File "xlwings\main.pyc", line 1727, in
File "xlwings\conversion__init__.pyc", line 3, in
File "pandas__init__.pyc", line 26, in
Main Features
File "pandas_libs__init__.pyc", line 4, in
File "pandas_libs\tslib.pyc", line 12, in
File "pandas_libs\tslib.pyc", line 10, in __load
File "pandas_libs\tslib.pyx", line 1514, in init pandas._libs.tslib
AttributeError: type object 'pandas._libs.tslib._TSObject' has no attribute 'reduce_cython'
Anyone have any clue?
Is there other way to let others run my python script on pc without python installed?
Thanks,