spatialaudio / python-sounddevice

:sound: Play and Record Sound with Python :snake:
https://python-sounddevice.readthedocs.io/
MIT License
982 stars 145 forks source link

PyInstaller fails: OSError: PortAudio library not found #378

Closed geoffr98 closed 2 years ago

geoffr98 commented 2 years ago

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?

geoffr98 commented 2 years ago

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/