Pyinstaller not working for package pyreadstat - python

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

Related

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.

Generated standalone *.exe from python is not runnable

Though the conversion from *.py- to a standalone *.exe-file via the command "pyinstaller --onefile filename.py" can be completed successfully, the executable itself failes to run
Traceback (most recent call last): File
"venv\Lib\site-packages\PyInstaller\loader\rthooks\pyi_rth_pkgres.py",
line 13, in File
"c:\users\user01\pycharmprojects\api4trd\venv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py",
line 623, in exec_module
exec(bytecode, module.dict) File "lib\site-packages\pkg_resources__init__.py", line 86, in
ModuleNotFoundError: No module named 'pkg_resources.py2_warn' [13560]
Failed to execute script pyi_rth_pkgres
following packages are used
1) math
2) datetime
3) yfinance
Python version: 3.8.1
pyinstaller version: 3.6
OS:W10
Thank you!
You are missing one hidden import in your pyinstaller command. Although earlier version of Python and Pyinstaller never used to give this error, but nevertheless here is the solution
pyinstaller --onefile --hidden-import=pkg_resources.py2_warn filename.py

how use pyinstaller with opencv in raspbian?

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

py2exe: run the compiled .exe, got error: type object 'pandas._libs.tslib._TSObject' has no attribute...__'

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,

Categories

Resources