Streamlit distribution not found in pyinstaller executable program - python

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

Related

Cannot create .exe with pyinstaller from .py with torchaudio (CPU): AttributeError: '_OpNamespace' 'torchaudio' object has no attribute 'cuda_version'

I have a .py script that uses torchaudio (without GPU) to process some sound in Windows. To distribute it, I've used pyinstaller to turn it into a .exe. You can reproduce the issue with this simple script:
import torchaudio
import time
if __name__ == '__main__':
t = torchaudio.transforms
time.sleep(3)
print("Success")
This script correctly runs from a python console python test.py but I want to create a test.exe that works in Windows (without having python installed). I create test.exe by using pyinstaller: pyinstaller test.py. This creates a build/test folder with all the required dependencies (around 1GB). test.exe is located inside that folder but when I double click on it, it fails with the following error:
Traceback (most recent call last):
File "torch\_ops.py", line 501, in __getattr__
op, overload_names = torch._C._jit_get_operation(qualified_op_name)
RuntimeError: No such operator torchaudio::cuda_version
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "torchaudio\__init__.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 499, in exec_module
File "torchaudio\_extension.py", line 136, in <module>
File "torchaudio\_extension.py", line 121, in _check_cuda_version
File "torch\_ops.py", line 505, in __getattr__
raise AttributeError(
AttributeError: '_OpNamespace' 'torchaudio' object has no attribute 'cuda_version'
[11648] Failed to execute script 'test' due to unhandled exception!
The environment uses:
python==3.9.15
torch==1.13.0
six==1.15.0
numpy==1.22.4
scipy==1.6.0
sounddevice==0.4.5
torchaudio==0.13.0
pyinstaller==5.6.2
Note: I tried the same installing torch with cuda ending up with the same error and a build 4 times bigger.
I was able to make the script work. Here are the steps I took to get it to run.
Create a new empty directory and pasted your script in as main.py
py -m venv venv && venv\scripts\activate && py -m pip install --upgrade pip pyinstaller
pip install torchaudio==0.13.0 torch==1.13.0 numpy=1.22.4 sounddevice==0.4.5 six==1.15.0 scipy
pyinstaller -F main.py
Go into venv\Lib\site-packages and copy the entire torchaudio folder and paste it into the top level directory alongside venv and main.py
In main.spec set datas=[('./torchaudio','./torchaudio')]
pyinstaller main.spec
And after compiling the executable runs... it still gives off a few warnings, but it runs and prints the success message.

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

Python EEL and PyInstaller Issue

I've recently created a program using eel, in this program I use the module moviepy to split the audio and the video of a file.
But when I try to put the whole project into an .exe file with PyInstaller, the app won't work.
The command I used is: python -m eel main.py web
I tried compiling the program without moviepy (I simply removed the import from main.py) and it perfectly worked.
So the problem is clearly moviepy which is not imported from PyInstaller.
Now I'm asking if there's any way to add moviepy dependencies using PyInstaller?
PS: Starting the source file works perfectly, it doesn't start only after compiled with PyInstaller
EDIT:
When I start the compiled .exe file with cmd, it gives me this error:
Traceback (most recent call last):
File "main.py", line 4, 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\Lucad\AppData\Local\Programs\Python\Python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\moviepy\editor.py", line 84, in <module>
File "<string>", line 1, in <module>
AttributeError: module 'moviepy.audio.fx.all' has no attribute 'audio_fadein'
[8740] Failed to execute script main
EDIT 2: FOUND A SOLUTION!
I read a thread where people say that moviepy has an error importing some modules, so all I had to do was modifying a file.
This is the link of the guide I followed: https://github.com/Zulko/moviepy/issues/591
Thanks to everybody answering me!
Did you try add moviepy explicitly to the path?
pyinstaller -y -D --name Whatever --clean --distpath="." -p "c:\Python37\Lib\site-packages\moviepy" yourpyfile.pyw

Error in running exe file having xgboost package by using pyinstaller

I have a code for predicting some value that uses xgboost package in the code. When I run it in PyCharm, it runs as expected.
The problem is when I make an executable file using pyinstaller. It will make the exe without any error, but when I run it the following error is raised:
Traceback (most recent call last):
File "test_fraud.py", line 3, in <module>
import xgboost
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\ShubhamSingh\PycharmProjects\cfna_scoring\venv\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 627, in
exec_module
exec(bytecode, module.__dict__)
File "lib\site-packages\xgboost\__init__.py", line 11, 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\ShubhamSingh\PycharmProjects\cfna_scoring\venv\lib\site-
packages\PyInstaller\loader\pyimod03_importers.py", line 627, in
exec_module
exec(bytecode, module.__dict__)
File "lib\site-packages\xgboost\core.py", line 161, in <module>
File "lib\site-packages\xgboost\core.py", line 123, in _load_lib
File "lib\site-packages\xgboost\libpath.py", line 48, in find_lib_path
xgboost.libpath.XGBoostLibraryNotFound: Cannot find XGBoost Library in
the candidate path, did you install compilers and run build.sh in root
path?
List of candidates:
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\xgboost\xgboost.dll
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\xgboost
\../../lib/xgboost.dll
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\xgboost\./lib/xgboost.dll
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\xgboost\xgboost.dll
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\xgboost
\../../windows/x64/Release/xgboost.dll
C:\Users\SHUBHA~1\AppData\Local\Temp\_MEI11402\
xgboost\./windows/x64/Release/xgboost.dll
[6564] Failed to execute script test_fraud
What's wrong here?
It seems that Pyinstaller can't find the xgboost.dll, VERSION files. So you need to add them manually to your output package. I also suggest you use a try/except block to see what is going on.
Suppose this simple example:
import traceback
try:
import xgboost
input("xgboost imported successfully!")
except Exception:
traceback.print_exc()
input("Import Error!")
I suggest you use an env to build your script so, you need to add the xgboost directory located at <path_to_venv>/xgboost and VERSION file located at <path_to_venv>/Lib/site-packages/xboost. Next, add them as a data-file with pyinstaller. Launch your env and execute the below command (My virtualenv named as env):
├───myscript.py
├───env
Command:
(env) > pyinstaller myscript.py -F --add-data "./env/xgboost/*;xgboost/" --add-data "./env/Lib/site-packages/xgboost/VERSION;xgboost/"

Pyinstaller: add custom module with ply as a dependence

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.

Categories