pyexcel hook missing in pyinstaller (unable to load pyexcel) - python

I am using below code to convert .xls file to .xlsx. It works fine when I run it normally or in debug mode, but If I convert this to the executable file(.exe ) the below code is not working. I guess pyinstaller doesn't have pyexcel hook to load the import. Is there any way I can add this import pyexcel to pyinstaller ??
import pyexcel
pyexcel.save_as(file_name=path_Xls, dest_file_name=path_Xls+'x')
I have Installed pyexcel-xls on my system, still getting this error
Error in console

Short Answer
Please append these lines to your command line:
--hidden-import pyexcel_xls --hidden-import pyexcel_xls.xls
--hidden-import pyexcel_xlsx --hidden-import pyexcel_xlsx.xlsx
Please also note, this solution works only with pyexcel-io v0.3.x and pyexcel v0.4.x
Long Answer
pyexcel-io plugins are lazy-loaded, in other words are imported when they are used. That is why pyinstaller cannot package the hidden imports into your executable by simply reading the source codes without knowing its runtime behaviour.
Here is the documentation on pyexcel-io v0.3.4 with pyinstaller
More information
With the latest pyexcel v0.5.0 and pyexcel-io 0.4.0, there are more options to be appended, I am afraid, because all built-in plugins become lazy-loaded too. Here are the corresponding documentations: pyinstaller on pyexcel and pyexcel-io

Related

Pyinstaller --hidden-imports not wirking

I'm trying to build an exe file from a python game using PyBox2D and Pyglet.
When I build the exe there occurs an error that a module cannot be imported:
Unable to import the back-end pyglet: module 'gui.backends' has no attribute 'pyglet_framework'
I guess this happens because this file only gets imported indirectly/hidden from another file with __import__()
This is my project hierarchy:
I've tried to add the file to pyinstaller on multiple ways. From the directory /SpaceJam/building I've tried calling:
pyinstaller.exe --onefile "../game/spacejam.py" --hidden-import="gui/framework/backends/pyglet_framework.py"
pyinstaller.exe --onefile "../game/spacejam.py" --hidden-import="../gui/framework/backends/pyglet_framework.py"
pyinstaller.exe --onefile "../game/spacejam.py" --hidden-import="../gui/framework/backends/pyglet_framework"
pyinstaller.exe --onefile "../game/spacejam.py" --hidden-import="../gui/framework/backends/*"
but none of that seemed to change anything in the error message.
I feel like I'm missing something obvious. Does somebody have an idea what I might be doing wrong or why the --hidden-import argument doesn't seem to work?
That argument expects a module name, not a filesystem path. How would you import it?
I'm not totally clear on how your project is set up, but try
--hidden-import gui.backends.pyglet_framework

pyinstaller binary not finding library files

I have a python program I'm converting to a single binary file and then moving to a different platform. When I run the binary it's complaining about a missing library. What's confusing is that it is present. How can I fix this error?
Binary Created:
pyinstaller --hidden-import pyaudio --hidden-import wave --hidden-import Logger --hidden-import json --hidden-import websockets --hidden-import asyncio --onefile Run_SD.py
Running Binary (on different platform):
/lib/libcrypto.so.1.1
/usr/lib/libcrypto.so.1.1
~$ ./Run_SD
[3656] Error loading Python lib '/tmp/_MEIgqfADy/libpython3.7m.so.1.0': dlopen: libcrypt.so.1: cannot open shared object file: No such file or directory
But the library is on the platform:
/lib/libcrypto.so.1.1
/usr/lib/libcrypto.so.1.1
(on the source plaform it's at: /usr/lib/aarch64-linux-gnu/libcrypto.so.1.1 )
I have found a solution .. not sure it's the correct one, but it works.
I copied the missing file into the source directory and then added --add-binary libcrypt.so.1:. to the command line which includes the file into the build and then all works.

PyInstaller SSL error with requests module - missing module ssl (_ssl)

In my python script, I use the requests module to call an API to GET and POST data.
Python environment: anaconda3, python 3.7 / packages: requests 2.24.0, pyinstaller 3.6, openssl 1.1.1h...
I have the following problem with the EXE-file generated by PyInstaller: When I run this file, I get the following error message. This error doesn't occur when I run my script from Python:
Traceback (most recent call last):
File "site-packages\PyInstaller\loader\rthooks\pyi_rth_certifi.py", line 13, in <module>
File "c:\programdata\anaconda3\envs\...\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
File "ssl.py", line 98, in <module>
ImportError: DLL load failed: The specified module could not be found.
[20188] Failed to execute script pyi_rth_certifi
If I follow to the excepted error lines (ssl.py):
import _ssl # if we can't import it, let the error propagate
Why can't ssl be imported?
I searched a long time in serveral post on SO but all these answers didn't help, ex.:
Python Requests throwing SSLError
Python Requests - How to use system ca-certificates (debian/ubuntu)?
Twilio Python Module Errors After Compiling
Fixing SSL certificate error in exe compiled with py2exe (or PyInstaller)
Does anyone have an idea how to fix this?
If you need more informations, please write a comment. THX
EDIT: (for Mooncrater's comment)
added Screenshot from python console, entered:
import _ssl
EDIT 2:
Tested "FIX" from SO Question:
Python SSL Import Error in PyInstaller generated executable
-> this didn't fix my problem
EDIT 3: Answer by cbolwerk
THX for your answer, this works!
Python 3.7 anaconda environment - import _ssl DLL load fail error
An additional fix is to install the latest OpenSSL Lib, I used 1.1.1h package in my python script, on my PC was installed an older version and that causes the error too.
cbolwerk's answer I tested on a PC where NO OpenSSL is installed and , as I wrote, it works!
If you can import ssl in the python within your environment, it means that the ssl module is probably inside your environment. This answer mentions the files you can look for inside your environment. They are either inside your env/Library/bin or env/DLLs. I think you have these installed, but pyinstaller doesn't recognize them. To make sure that pyinstaller knows these files, you can add them to datas. This can be edited in the command when building the .exe file or in the .spec file that is probably created when you have run this command once. This link may be useful there.
In short, add the paths of the DLLs I mentioned to the datas (or binaries inside .spec actually) so that they are recognized by pyinstaller.
So either run the command
pyinstaller --onedir --add-data libcrypto-1_1-x64.dll;. --add-data libssl-1_1-x64.dll;. myscript.py
or change your .spec to something like
datas = [('/path/to/libcrypto-1_1-x64.dll', '.'), ('/path/to/libssl-1_1-x64.dll', '.'),
(...) ]
...
a = Analysis(...,
datas=datas,
...)
...
and then run
pyinstaller --onedir myscript.spec
This fixed DLL issues for me at least.

openpyxl not working as EXE

Error message from running my exe:
ModuleNotFoundError: No module named 'openpyxl'
testHi.py
#simple test to see if openpyxl module works
import openpyxl
print ("hi")
input()
hook-openpyxl.py
# taken from pyinstaller dev team, store in same dir as testHi.py
from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('openpyxl')
cmd line input:
pyinstaller.exe --onefile --icon=py.ico --additional-hooks-dir=. hiTest.py
I run the the hiTest and get the error above.
I have looked everywhere for this solution. Can anyone tell me what I am doing wrong.
I fixed my issue by installing it through Pip, rather than install the package through Pycharm, and Pyinstaller was able to find the package.
I got the idea from looking through the text in the command prompt and saw it was loading modules that I had installed via Pip and not through Pycharm.
I was able to get this working using auto-py-to-exe (which uses pyinstaller) by including the following folders/files from my python library into the same folder that I run pyinstaller from:
jdcal.py
openpyxl (folder)
et_xmlfile (folder)
pyinstaller command:
pyinstaller -y -F "[directory]/myscript.py"
Notes on Library Location:
Windows library location for me was: C:\users[username]\AppData\Local\Programs\Python\Python37-32\Lib
The packages were in the "site_packages" folder
use
--hiddenimport openpyxl
long with the previous solutions, it worked for me, I was able to enforce the import of openpyxl.
You was quite close. :-)
I fixed the problem by modifying the "hook-openpyxl.py" file. The command collect_data_files('openpyxl') actually returns an empty list. But there is another command collect_submodules which seems to do what we want. So my "hook-openpyxl.py" file looks like this.
from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('openpyxl')
I placed the "hook-openpyxl.py" file in the same directory like my spec file. In the spec file I set to location of the new hook file
a = Analysis(
...
...
hookspath=['.'],
...
...
...
I guess, you will have the same result with your command line parameter
pyinstaller.exe --onefile --icon=py.ico --additional-hooks-dir=. hiTest.py
My environment
Python=3.8
openpyxl=3.0.10
PyInstaller=4.8

Any pyinstaller detailed example about hidden import for psutil?

I want to compile my python code to binary by using pyinstaller, but the hidden import block me. For example, the following code import psutil and print the CPU count:
# example.py
import psutil
print psutil.cpu_count()
And I compile the code:
$ pyinstaller -F example.py --hidden-import=psutil
When I run the output under dist:
ImportError: cannot import name _psutil_linux
Then I tried:
$ pyinstaller -F example.py --hidden-import=_psutil_linux
Still the same error. I have read the pyinstall manual, but I still don't know how to use the hidden import. Is there a detailed example for this? Or at least a example to compile and run my example.py?
ENVs:
OS: Ubuntu 14.04
Python: 2.7.6
pyinstaller: 2.1
Hi hope you're still looking for an answer. Here is how I solved it:
add a file called hook-psutil.py
from PyInstaller.hooks.hookutils import (collect_data_files, collect_submodules)
datas = [('./venv/lib/python2.7/site-packages/psutil/_psutil_linux.so', 'psutil'),
('./venv/lib/python2.7/site-packages/psutil/_psutil_posix.so', 'psutil')]
hiddenimports = collect_submodules('psutil')
And then call pyinstaller --additional-hooks-dir=(the dir contain the above script) script.py
pyinstall is hard to configure, the cx_freeze maybe better, both support windows (you can download the exe directly) and linux. Provide the example.py, In windows, suppose you have install python in the default path (C:\\Python27):
$ python c:\\Python27\\Scripts\\cxfreeze example.py -s --target-dir some_path
the cxfreeze is a python script, you should run it with python, then the build files are under some_path (with a lot of xxx.pyd and xxx.dll).
In Linux, just run:
$ cxfreeze example.py -s --target-dir some_path
and also output a lot of files(xxx.so) under some_path.
The defect of cx_freeze is it would not wrap all libraries to target dir, this means you have to test your build under different environments. If any library missing, just copy them to target dir. A exception case is, for example, if your build your python under Centos 6, but when running under Centos 7, the missing of libc.so.6 will throw, you should compile your python both under Centos 7 and Centos 6.
What worked for me is as follows:
Install python-psutil: sudo apt-get install python-psutil. If you
have a previous installation of the psutil module from other
method, for example through source or easy_install, remove it first.
Run pyinstaller as you do, without the hidden-import option.
still facing the error
Implementation:
1.python program with modules like platform , os , shutil and psutil
when i run the script directly using python its working fine.
2.if i build a binary using pyinstaller. The binary is build successfully. But if i run the binary iam getting the No module named psutil found.I had tried several methods like adding the hidden import and other things. None is working. I trying it almost 2 to 3 days.
Error:
ModuleNotFoundError: No module named 'psutil'
Command used for the creating binary
pyinstaller --hidden-import=['_psutil_linux'] --onefile --clean serverHW.py
i tried --additional-hooks-dir= also not working. When i run the binary im getting module not found error.

Categories

Resources