Module not found after building python project by using pysinstaller - python

I recently finished a project I have been working on for a while. I should present my work on the class' smart board which runs on linux. Taking in mind i can't download its modules because i don't have sudo permission nor it does have internet. So to get around this I decided to build it using pyinstaller. Everything seemed to work just fine in my device, but when i double checked in Virtual box this error pops up.
Traceback (most recent call last):
File "main.py", line 4, 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/pyimod03_importers.py", line 540, in exec_module
File "matplotlib/__init__.py", line 923, in <module>
File "matplotlib/__init__.py", line 608, in matplotlib_fname
RuntimeError: Could not find matplotlibrc file; your Matplotlib install is broken
[8025] Failed to execute script main
So how can i build it so it works for all devices without the need of installation for any modules

matplotlib requires a config file names matplotlibrc to be found in one of 4 specific locations, the first one being the courant directory (see https://matplotlib.org/tutorials/introductory/customizing.html#the-matplotlibrc-file) One this file prepared as per need it can be embeeded in the exe by adding --add-data=matplotlibrc;. to the build command (replace ; with : for non-Windows systems)

Related

Error when packaging eel project to an exe

I am building an python project with Eel, and I'm trying to package my program to an exe with PyInstaller as instructed in the documentation. However, I get the following error when I open the .exe:
Error
Traceback (most recent call last):
File "hello.py", line 1, in <module>
import eel
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 "eel\__init__.py", line 8, in <module>
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 "bottle.py", line 73, in <module>
AttributeError: 'NoneType' object has no attribute 'write'
I tried to use auto py-to-exe instead of Pyinstaller, but I got the same error. Finally, I tried to package the Eello world example from the Eel documentation to see if it was my program that was the issue, but I still got the same error. I found a similiar problem in a github issue, but I fail to see what I should attach to --add-data.
I am running Python 3.10.8 and Eel 0.14.0. I've now tried python 3.8.9 and python 3.9.13 without luck.
Update
Upon further investigation, I discovered that the error only occours when I attach --noconsole at the end of my cmd python -m eel hello.py web --onefile --noconsole . However, I want my app to work without a visible console.
One of the libraries you are using is attempting to write to sys.stdout and sys.stderr, which are set to None when you run pyinstaller with --windowed option.
You need to explicitly set sys.stderr and sys.stdout in your programs code as early as possible to a writeable object like an open file or an io buffer.
for example:
import sys
outfile = open("logfile.txt", "wt")
sys.stderr = outfile
sys.stdout = outfile

How to include PortAudio into a pyinstaller onefile build

I am using sounddevice in my project running on Ubuntu - When I build a single file using pyinstaller it is failing with (The code works well when running the source python file):
Traceback (most recent call last):
File "Run_SD.py", line 2, in <module>
File "Smart_Device.py", line 26, in init Smart_Device
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 "PyInstaller/loader/pyimod03_importers.py", line 540, in exec_module
File "sounddevice.py", line 71, in <module>
OSError: PortAudio library not found
[213] Failed to execute script Run_SD
Unfortunately I can't simply apt get install the required portaudio on the machine, so it needs to be included in the package. I have tried adding in the lib using:
--add-binary libportaudio.so:. --add-binary libportaudio.a:.
But this isn't working. I'm guessing I need to actually import the portaudio library into my python source - is that correct? Any pointers on how to do this?
I fixed this problem by editing:
versions/3.8.5/lib/python3.8/site-packages/_pyinstaller_hooks_contrib/hooks/stdhooks/hook-sounddevice.py
elif is_linux:
path = os.path.join(
sfp[0], "_sounddevice_data", "portaudio-binaries", "libportaudio.so"
)
versions/3.8.5/lib/python3.8/site-packages/sounddevice.py
elif _platform.system() == 'Linux':
_libname = 'libportaudio.so'
Then included the libportaudio.so in the pyinstaller build:
--add-binary _sounddevice_data/portaudio-binaries/libportaudio.so:./_sounddevice_data/portaudio-binaries/

Permission error importing custom functions in Python Anaconda3 Mac OS Catalina

Set-up
After updating to Catalina, Anaconda3 trouble commenced.
I've managed to solve most errors, doing a fresh install as advised by Anaconda, but can't seem to get around a permission error when trying to importing functions from other .py files – something that worked just fine before I upgraded to Catalina.
I run Python 3.7 from Spyder 3.3.6.
Error
Whenever I try to import a custom function, I get,
import os
os.chdir('/Users/mypath/payment_management')
from payment_knab import pay_knab
Traceback (most recent call last):
File "<ipython-input-1-cb99b6800e4e>", line 3, in <module>
from payment_knab import pay_knab
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 963, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 906, in _find_spec
File "<frozen importlib._bootstrap_external>", line 1280, in find_spec
File "<frozen importlib._bootstrap_external>", line 1249, in _get_spec
File "<frozen importlib._bootstrap_external>", line 1213, in _path_importer_cache
PermissionError: [Errno 1] Operation not permitted
Question
How do I run Anaconda/Spyder/Python with admin permission? Which command in terminal do I need to execute?
Stupidly enough I have actually managed to run with admin permission before via Terminal, but I forgot how...
I've been looking at StackOverflow for an hour now, but can't find anything that helps.
You've probably forgotten again how to do this. Use sudo spyder, dummy.

How import 'html5lib' package to Python 3.5 Script in Azure Machine Learning Studio?

I'm trying to import the package html5lib to Azure Machine Learning, into a Execute Python Script module. It's a similar question as here, but same solution didn't work... :/
My steps until now:
I got the latest version of HTML5LIB package from the project website;
Unzip tar.gz file and re-zip as .ZIP file;
Upload the file in Azure Studio as a Dataset named as 'html5lib.zip';
enter image description here
Add the zip file as dataset into my Experiment and connected to Script Bundle Input;
enter image description here
Run the following script:
enter image description here
Then, I got the error:
Error 0085: The following error occurred during script evaluation, please view the output log for more information:
---------- Start of error message from Python interpreter ----------
Caught exception while executing function: Traceback (most recent call last):
File "C:\server\invokepy.py", line 189, in batch
mod = import_module(moduleName)
File "C:\pyhome\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 662, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "C:\temp\6d718116a1f549e59a5b96c5d6360911.py", line 22, in <module>
import html5lib
ImportError: No module named 'html5lib'
Process returned with non-zero exit code 1
Any idea how can I fix this?
Thanks in advance.
This worked for me. Ran the following commands on windows command line.
mkdir html5lib
# cd into above directory
pip install html5lib --target=.
# Zip the contents of the directory html5lib.zip
This is code in Python Script Module
This is screenshot of the experiment and success message after executing the experiment

PyInstaller error "Python not installed as framework"

I am working on MacOS and I am trying to use PyInstaller to circulate a script to some non tech colleagues. The script is imaginatively called script_V1.py and runs fine.
Up to now, I have had issues with PANDAS and SCIPY but they seem to be resolved with with the command
pyinstaller --onefile --hidden-import pandas._libs.tslibs.timedeltas --hidden-import scipy._lib.messagestream script_V1.py
However, I am now getting a different error (I'll post the full error below)
RuntimeError: Python is not installed as a framework. The Mac OS X
backend will not be able to function correctly if Python is not
installed as a framework. See the Python documentation for more
information on installing Python as a framework on Mac OS X. Please
either reinstall Python as a framework, or try one of the other
backends. If you are using (Ana)Conda please install python.app and
replace the use of 'python' with 'pythonw'. See 'Working with
Matplotlib on OSX' in the Matplotlib FAQ for more information.
I have tried the solution in a similar question on Stack Exchange:
Installation Issue with matplotlib Python
But I think that is actually a different problem with import statements not working within the script itself. In my case if I run the script as a .py script it works fine. Anyway adding the suggested file did not alter the error message. Im new to this and seem to be really struggling with PyInstaller (more than with python itself) so any help much appreciated.
The full error message is (I've added line breaks for clarity)
Traceback (most recent call last):
File "script_V1.py", line 5, in <module>
import matplotlib.pyplot as plt
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PyInstaller/loader/pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packages/matplotlib/pyplot.py", line 115, in <module>
File "site-packages/matplotlib/backends/__init__.py", line 62, in pylab_setup
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PyInstaller/loader/pyimod03_importers.py", line 631, in exec_module
exec(bytecode, module.__dict__)
File "site-packages/matplotlib/backends/backend_macosx.py", line 17, in <module>
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1191, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1161, in _load_backward_compatible
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PyInstaller/loader/pyimod03_importers.py", line 714, in load_module
module = loader.load_module(fullname)
RuntimeError: Python is not installed as a framework. The Mac OS X
backend will not be able to function correctly if Python is not
installed as a framework. See the Python documentation for more
information on installing Python as a framework on Mac OS X. Please
either reinstall Python as a framework, or try one of the other
backends. If you are using (Ana)Conda please install python.app and
replace the use of 'python' with 'pythonw'. See 'Working with
Matplotlib on OSX' in the Matplotlib FAQ for more information.
[2582] Failed to execute script script_V1
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
So the way I got this to work in the end was to force Matplotlib to use WebAGG and also install the 'tornado' package (which is needed for some reason).

Categories

Resources