Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1234

Python • Python Sounddevice problems

$
0
0
Hi guys,
I'm currently working on a project where i have a RPi 5 that runs a python script to genreate sound and output it using a blutooth speaker.
I have a virtual environment on my RPi that has all needed dependencies and I'm using the sounddevice python library to play continuous sound with the sounddevice.OutputStream method.

When i now activate my venv and run the script as my User, everything works fine. If i, however, try to run it as root using sudo i get this error:

Traceback (most recent call last):
File "/home/tesremin/Documents/Bachelor2024W/TeslaCoilOutput.py", line 39, in <module>
with sound.OutputStream(channels=1, samplerate=44100, callback=audiocallback):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/tesremin/Documents/Bachelor2024W/venv/lib/python3.11/site-packages/sounddevice.py", line 1515, in _init
StreamBase.init(self, kind='output', wrapcallback='array',
File "/home/tesremin/Documents/Bachelor2024W/venv/lib/python3.11/site-packages/sounddevice.py", line 828, in __init
_get_stream_parameters(kind, device, channels, dtype, latency,
File "/home/tesremin/Documents/Bachelor2024W/venv/lib/python3.11/site-packages/sounddevice.py", line 2708, in _get_stream_parameters
info = query_devices(device)
^^^^^^^^^^^^^^^^^^^^^
File "/home/tesremin/Documents/Bachelor2024W/venv/lib/python3.11/site-packages/sounddevice.py", line 572, in query_devices
raise PortAudioError(f'Error querying device {device}')
sounddevice.PortAudioError: Error querying device -1

And also sometimes the error: "ModuleNotFoundError: No module named 'sounddevice'"

From what i got from other websites, forums etc. the Problem is probably that the root user doesnt use my venv python version. To bypass this i tried what everyone recommended including giving the exact path to my venv for sudo to use, using PYTHONPATH etc. but nothing is working.
It's like i am the only person on the internet that struggles with this. I need to run it with sudo because of a few libraries that i have to use that require sudo and also because i want to auto start the script on boot which yields the same error (presumably because that too tries to use the wrong python version namely the one in /usr/bin/python

i have also tried just installing the sounddevice lib globally but rpi5 does not allow that using pip and apt install just won't work, 0 clue why.

So yeah thats basically it, when i try to run my script using root (or autostart with crontab etc.) it does not use the venv even when i specify it with the tons of options that are available to me.

PS. I already tried just reinstalling the OS and wiping the slate clean, didn't help.

Thanks a lot in advance, i'm really at the end of my ropes here. Happy to share anything else that might help.

Statistics: Posted by Tesremin — Sun Dec 08, 2024 11:15 pm



Viewing all articles
Browse latest Browse all 1234

Trending Articles