How to include PortAudio into a pyinstaller onefile build - python

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/

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

Issue with virtualenv/mkvirtualenv/pip - "zsh: /usr/local/bin/pip: bad interpreter: /usr/local/opt/python#2/bin/python2.7: no such file or directory"

I'm trying to create a new virtualenvironment for some repos but seem to be running into a bunch if issues in my terminal, these are the errors I get:
zsh: /usr/local/bin/pip: bad interpreter: /usr/local/opt/python#2/bin/python2.7: no such file or directory
OR this issue:
ERROR: It thinks sys.prefix is '/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7' (should be '/Users/USERNAME_HERE/.virtualenvs/returns-backend')
ERROR: virtualenv is not compatible with this system or executable
I get this error when running: brew link --overwrite python from here
Linking /usr/local/Cellar/python/3.7.7... Error: Cannot link python
Another version is already linked: /usr/local/Cellar/python/3.7.6_1
I also get this issue when running the following command: pip3 install --upgrade pip from this comment
Traceback (most recent call last):
File "/usr/local/bin/pip3", line 11, in <module>
load_entry_point('pip==19.3.1', 'console_scripts', 'pip3')()
File "/usr/local/lib/python3.7/site-packages/pip/_internal/main.py", line 45, in main
command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
File "/usr/local/lib/python3.7/site-packages/pip/_internal/commands/__init__.py", line 96, in create_command
module = importlib.import_module(module_path)
File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
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 "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/usr/local/lib/python3.7/site-packages/pip/_internal/commands/install.py", line 23, in <module>
from pip._internal.cli.req_command import RequirementCommand
File "/usr/local/lib/python3.7/site-packages/pip/_internal/cli/req_command.py", line 17, in <module>
from pip._internal.index import PackageFinder
ImportError: cannot import name 'PackageFinder' from 'pip._internal.index' (/usr/local/lib/python3.7/site-packages/pip/_internal/index/__init__.py)
I haven't touched the terminal in a long time (2+ years) and don't remember what I used to set these up as my documentation wasn't great last time when starting to code. I think I remember using brew before to set this stuff all up, but now I'm just worried about making any more changes that might break things even further.
Any help with commands that might not be as intrusive/possibly making this problem bigger?

Module not found after building python project by using pysinstaller

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)

Cairosvg with pyinstaller "No such file or directory:"

I'm using python 3.7 and I want to create an executable from a script that use "cairosvg" package, the exe gives me this error while trying to run it:
Traceback (most recent call last):
File "testCairo.py", line 1, 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 "PyInstaller\loader\pyimod03_importers.py", line 531, in exec_module
File "cairosvg\__init__.py", line 22, in <module>
File "pathlib.py", line 1199, in read_text
File "pathlib.py", line 1186, in open
File "pathlib.py", line 1039, in _opener
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\moham\\AppData\\Local\\Temp\\_MEI135122\\cairosvg\\VERSION'
[16072] Failed to execute script testCairo
any one has an idea how to solve this error ?
my code is only import cairosvg as the following:
import cairosvg
print('test')
Did you try to include the cairosvg folder in the pyinstaller command?
Pyinstaller --add-binary <path to cairosvg>;cairosvg testCairo.py
The package should be in your python installation folder, something like: "C:\Users<User>\AppData\Local\Programs\Python\Python37\Lib\site-packages\cairosvg".
The command should copy the cairosvg package folder from its location into your exe's dist folder. If you prefer one single file, you could also add "--onefile" to the pyinstaller command.

Encounter Error after installing Pytorch using pip in python3.5

I am using windows 10 OS with Python 3.5.
I am trying to run a program related to torch. So I installed Pytorch with the help of the instructions specified on their website: Pytorch installation
See the following image for selection of the downloadable:
I installed it using :
pip install http://download.pytorch.org/whl/cpu/torch-0.4.0-cp35-cp35m-win_amd64.whl
pip install torchvision
I have encountered the following error:
Error
Traceback (most recent call last):
File "C:\Python35\lib\site-packages\twisted\trial\runner.py", line 701, in findByName
__import__(name)
File "C:\Users\aims\Desktop\CNN\source\cnn4matrix.py", line 4, in <module>
import torch
File "C:\Python35\lib\site-packages\torch\__init__.py", line 78, in <module>
from torch._C import *
ImportError: DLL load failed: The specified module could not be found.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python35\lib\site-packages\twisted\trial\runner.py", line 803, in loadByName
return self.suiteFactory([self.findByName(name, recurse=recurse)])
File "C:\Python35\lib\site-packages\twisted\trial\runner.py", line 704, in findByName
return self.loadFile(_name, recurse=recurse)
File "C:\Python35\lib\site-packages\twisted\trial\runner.py", line 674, in loadFile
module = SourceFileLoader(name, fileName).load_module()
File "<frozen importlib._bootstrap_external>", line 385, in _check_name_wrapper
File "<frozen importlib._bootstrap_external>", line 806, in load_module
File "<frozen importlib._bootstrap_external>", line 665, in load_module
File "<frozen importlib._bootstrap>", line 268, in _load_module_shim
File "<frozen importlib._bootstrap>", line 693, in _load
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:\Users\aims\Desktop\CNN\source\cnn4matrix.py", line 4, in <module>
import torch
File "C:\Python35\lib\site-packages\torch\__init__.py", line 78, in <module>
from torch._C import *
ImportError: DLL load failed: The specified module could not be found.
After searching a lot I couldn't found anything related. Instead I found that I have tried: Reinstall with upgrade mode, the numpy library.
Please let me know what I can try.
EDITED:
This is the scenario for me: I have install torch from the website instructions and then opened python terminal. just written import torch and the issue is what I got. Have a look at the image: https://ibb.co/fL53HT
Please see this following thread:
https://github.com/pytorch/pytorch/issues/574
Basically, the problem might be that you have a folder called 'torch' in the same directory from where you are invoking python. So instead of the pytorch library being loaded, the folder named 'torch' is being picked up.
Do this: cd .. (to change directory), and then start python and import torch, it should work.
the one solution here if doesn't work after reinstalling Anaconda, then you can use sth. like Dependency Walker, open
[Anaconda PATH]\Lib\site-packages\torch\lib\_C.pyd
to detect what's wrong there.
and the other solution is caused by the directory torch which is generated in the same directory by compiling the source. try to simply changing the directory before open python.

Categories

Resources