Pyinstaller: add custom module with ply as a dependence - python

I have a custom package, an egg installed in virtualenv and saved also in binaries folder. I tried to use it inside a program with pyinstaller. My spec looks like this:
a = Analysis(['main.py'],
pathex=['/[...]/myproject', '/[...]/virtualenv/My_Library-3.18.1-py3.5.egg'],
[...])
hiddenimports=['mylibrary', 'mylibrary.providers', 'mylibrary.tools'],
But when I put in the code:
import mylibrary as external
It gives me the error:
Traceback (most recent call last):
File "main.py", line 3, in <module>
import mylibrary as external
File "<frozen importlib._bootstrap>", line 968, in _find_and_load
File "<frozen importlib._bootstrap>", line 957, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked

Put the mylibrary to hiddenimports in your spec file.
hiddenimports = [mylibrary]

I found the solution. I didn't paste all the trace, at the end it finished with:
File "/home/clara/enviroments/egggui/lib/python3.5/site-packages/PyInstaller-3.4-py3.5.egg/PyInstaller/loader/pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages/pdfminer3k-1.3.1-py3.5.egg/pdfminer/pslexer.py", line 138, in <module>
File "site-packages/ply-3.8-py3.5.egg/ply/lex.py", line 906, in lex
File "site-packages/ply-3.8-py3.5.egg/ply/lex.py", line 580, in validate_all
File "site-packages/ply-3.8-py3.5.egg/ply/lex.py", line 822, in validate_rules
File "site-packages/ply-3.8-py3.5.egg/ply/lex.py", line 833, in validate_module
File "inspect.py", line 936, in getsourcelines
File "inspect.py", line 767, in findsource
OSError: could not get source code
[17622] Failed to execute script main
I did a research of lex.py line 833 and I found a bug for version 3.8: https://github.com/dabeaz/ply/issues/97 So I updated ply to version 3.9 and error disapeared.
If you can't upgrade ply to version 3.9, some people said something of adding the file to spec, but I didn't get it... here is the question where they gave that advice.

Related

converted py to exe using pyinstaller but when run it getting openpyxl module missing?

I did pyinstaller --onefile main.py in command prompt
This made the exe in a folder called dist where my project is which I then ran in command prompt and got the error below:
DevTools listening on ws://127.0.0.1:49451/devtools/browser/b4cedddd-0d98-4929-8fdb-b92d174992cc
[18704:11556:1103/114958.968:ERROR:device_event_log_impl.cc(214)] [11:49:58.968] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
[18704:11556:1103/114958.968:ERROR:device_event_log_impl.cc(214)] [11:49:58.968] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)
Traceback (most recent call last):
File "pandas\compat\_optional.py", line 138, in import_optional_dependency
File "importlib\__init__.py", line 126, in import_module
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "openpyxl\__init__.py", line 6, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "openpyxl\workbook\__init__.py", line 4, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "openpyxl\workbook\workbook.py", line 9, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "openpyxl\worksheet\_write_only.py", line 13, in <module>
File "openpyxl\worksheet\_writer.py", line 23, in init openpyxl.worksheet._writer
ModuleNotFoundError: No module named 'openpyxl.cell._writer
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 1, in <module>
import newspapers as np
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "newspapers.py", line 16, in <module>
from constants import driver
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "constants.py", line 9, in <module>
df = pd.read_excel('MOPI Template News & Mags.xlsx', sheet_name='Passwords')
File "pandas\util\_decorators.py", line 311, in wrapper
File "pandas\io\excel\_base.py", line 457, in read_excel
File "pandas\io\excel\_base.py", line 1419, in __init__
File "pandas\io\excel\_openpyxl.py", line 524, in __init__
File "pandas\compat\_optional.py", line 141, in import_optional_dependency
ImportError: Missing optional dependency 'openpyxl'. Use pip or conda to install openpyxl.
[2500] Failed to execute script 'main' due to unhandled exception!
I'm using python version 3.10 - i coded this program in a virtual environment where i have all the modules. When i run pip list it shows I do have openpyxl 3.0.10.
I saw a suggestion to try update the auto generated spec file and use hidden_imports variable for the modules that are missing - so I followed a video online which advised to update lines so had below added/amended:
from PyInstaller.utils.hooks import collect_submodules
hidden_imports = collect_submodules('openpyxl')
hiddenimports=hidden_imports,
I got the exact same error after trying this.
The program runs fine when in PyCharm but not when run this exe I've created. I also made sure to try run using the terminal in PyCharm too. Any ideas why I'm still getting this error?
When you open the dist folder, do you see the openpyxl module? If not, try to do it via the add-data or hidden-import. Or even easier, just copy paste it manually and see if that works. Like pyinstaller file.py --hidden-import=openpyxl
You could either try updating your pyhooks-contrib as it could have been fixed

NameError: name 'defaultParams' is not defined while running the .exe converted using Pyinstaller

I successfully converted the main.py script using pyinstaller. However the below stated error is thrown while executing the .exe file for the same.
MatplotlibDeprecationWarning: Matplotlib installs where the data is not in the mpl-data subdirectory of the package are deprecated since 3.2 and support for them will be removed two minor releases later.
exec(bytecode, module.__dict__)
Traceback (most recent call last):
File "main.py", line 8, in <module>
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "c:\users\XXXX\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
exec(bytecode, module.__dict__)
File "matplotlib\__init__.py", line 898, in <module>
File "matplotlib\cbook\__init__.py", line 480, in _get_data_path
File "matplotlib\__init__.py", line 239, in wrapper
File "matplotlib\__init__.py", line 534, in get_data_path
File "matplotlib\__init__.py", line 239, in wrapper
File "matplotlib\__init__.py", line 563, in _get_data_path
NameError: name 'defaultParams' is not defined
Supposedly, all you can do to fix this is to downgrade your matplotlib to the version:
pip install matplotlib==3.2.2
Apparently, the latest version of matplotlib meddles with the environment variable in the windows path.

Missing dill was found using pyinstaller to compile executable file

I built a small program using sklearn, numpy and pandas, I tried to compile the file but it failed and have the following error message, it said there was a missing dll file in some location. I looked at this thread.
File "site-packages\PyInstaller\loader\pyiboot01_bootstrap.py", line 174, in __init__
File "ctypes\__init__.py", line 373, in __init__
FileNotFoundError: Could not find module 'C:\Users\Dennis\AppData\Local\Temp\_MEI101722\sklearn\.libs\vcomp140.dll' (or one of its dependencies). Try using the full path with constructor syntax.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "sklearn111.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "c:\users\dennis\appdata\local\programs\python\python38-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\sklearn\__init__.py", line 78, in <module>
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "c:\users\dennis\appdata\local\programs\python\python38-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\sklearn\_distributor_init.py", line 18, in <module>
File "site-packages\PyInstaller\loader\pyiboot01_bootstrap.py", line 176, in __init__
__main__.PyInstallerImportError: Failed to load dynlib/dll 'C:\\Users\\Dennis\\AppData\\Local\\Temp\\_MEI101722\\sklearn\\.libs\\vcomp140.dll'. Most probably this dynlib/dll was not found when the application was frozen.
[6572] Failed to execute script gui_plus_model
So I put the dll it needs at the same folder with the .py file to be compiled, but the same error exist. The version of python is 3.8 and all modules needed have already been upgraded to the latest version.
pyinstaller -F --add-binary "vcomp140.dll;.dll" gui_plus_model.py

Streamlit distribution not found in pyinstaller executable program

I made a streamlit application to act as a front end for a forecasting program and then I created a pyinstaller executable file to run the application. The problem rises when I run the executable file, I get the following error: pkg_resources.DistributionNotFound: The 'streamlit' distribution was not found and is required by the application with this traceback:
Traceback (most recent call last):
File "main.py", line 8, in <module>
File "<frozen importlib._bootstrap>", line 968, in _find_and_load
File "<frozen importlib._bootstrap>", line 957, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "/opt/anaconda3/envs/Python35/lib/python3.5/site-packages/PyInstaller/loader/pyimod03_importers.py", line 623, in exec_module
exec(bytecode, module.__dict__)
File "site-packages/streamlit/__init__.py", line 75, in <module>
File "site-packages/pkg_resources/__init__.py", line 476, in get_distribution
File "site-packages/pkg_resources/__init__.py", line 352, in get_provider
File "site-packages/pkg_resources/__init__.py", line 895, in require
File "site-packages/pkg_resources/__init__.py", line 781, in resolve
pkg_resources.DistributionNotFound: The 'streamlit' distribution was not found and is required by the application
[48095] Failed to execute script main
The file was created with the following command: pyinstaller --onefile -w main.py
This was setup on macOS Catalina with python 3.5.6 within a Conda environment
If there are any details that I may have missed that would help with this, please let me know.
Looks like you need to add a hook file called hook-streamlit.py with the following contents
from PyInstaller.utils.hooks import copy_metadata
datas = copy_metadata('streamlit')
You also need to pass the --additional-hooks-dir flag to your pyinstaller command.
If you've placed the hook file in the same directory as your python script you can specify it like so
pyinstaller --onefile --additional-hooks-dir=. -w main.py

How to add lib_lightgbm.dll to exe when using pyinstaller to freeze python code

I am creating a single-file executable of a python code that imports Microsoft's LightGBM. I used the following command to create the exe:
pyinstaller -F --hidden-import numpy.core._dtype_ctypes pro.py
After creating the exe and running it, the following error occurs:
Exception: Cannot find lightgbm library file in following paths:
C:\Users\...\AppData\Local\Temp\_MEI61682\lightgbm\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\_MEI61682\lightgbm\compile\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\_MEI61682\compile\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\lib\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\_MEI61682\compile\Release\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\_MEI61682\compile\windows\x64\DLL\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\Release\lib_lightgbm.dll
C:\Users\...\AppData\Local\Temp\windows\x64\DLL\lib_lightgbm.dll
[6352] Failed to execute script pro
I tried adding lib_lightgbm.dll using the option :
--add-data lib_lightgbm.dll;\compile
This resolves the error on the machine that lightgbm is installed, but on other machines, it gives the following error:
Traceback (most recent call last):
File "site-packages\PyInstaller\loader\pyiboot01_bootstrap.py", line 149, in __init__
File "ctypes\__init__.py", line 348, in __init__
OSError: [WinError 126] The specified module could not be found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "pro.py", line 9, in <module>
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "c:\users\...\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
File "site-packages\lightgbm\__init__.py", line 8, in <module>
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "c:\users\...\appdata\local\programs\python\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
File "site-packages\lightgbm\basic.py", line 34, in <module>
File "site-packages\lightgbm\basic.py", line 29, in _load_lib
File "ctypes\__init__.py", line 426, in LoadLibrary
File "site-packages\PyInstaller\loader\pyiboot01_bootstrap.py", line 151, in __init__
__main__.PyInstallerImportError: Failed to load dynlib/dll 'C:\\Users\\...\\AppData\\Local\\Temp\\_MEI7562\\lightgbm\\../compile\\lib_lightgbm.dll'. Most probably this dynlib/dll was not found when the application was frozen.
[4060] Failed to execute script pro
Can anyone help me resolve this issue?

Categories