question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'import soundcard' throws an error when run through a systemd service

See original GitHub issue

Hey, I have the following code:

#test.py
import soundcard
while True:
	pass

It runs fine with python test.py

But it does not when I want to run it using a systemd service.

Here’s the service definition:

[Unit]
Description=Soundcard test service
Requires=sound.target
After=sound.target

[Service]
ExecStart=python test.py
User=pi

[Install]
WantedBy=default.target

And the error it throws:

pi@raspberrypi:sudo systemctl start test.service
pi@raspberrypi:~ $ sudo systemctl status test.service
● caravane.service - Soundcard test service
   Loaded: loaded (/lib/systemd/system/test.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2021-07-26 20:58:23 CEST; 3s ago
  Process: 2649 ExecStart=/usr/bin/python /home/pi/test.py (code=exited, status=1/FAILURE)
 Main PID: 2649 (code=exited, status=1/FAILURE)

juil. 26 20:58:23 raspberrypi python[2649]:     import soundcard
juil. 26 20:58:23 raspberrypi python[2649]:   File "/home/pi/.local/lib/python3.7/site-packages/soundcard/__init__.py", line 4, in <module>
juil. 26 20:58:23 raspberrypi python[2649]:     from soundcard.pulseaudio import *
juil. 26 20:58:23 raspberrypi python[2649]:   File "/home/pi/.local/lib/python3.7/site-packages/soundcard/pulseaudio.py", line 261, in <module>
juil. 26 20:58:23 raspberrypi python[2649]:     _pulse = _PulseAudio()
juil. 26 20:58:23 raspberrypi python[2649]:   File "/home/pi/.local/lib/python3.7/site-packages/soundcard/pulseaudio.py", line 72, in __init__
juil. 26 20:58:23 raspberrypi python[2649]:     assert self._pa_context_get_state(self.context)==_pa.PA_CONTEXT_READY
juil. 26 20:58:23 raspberrypi python[2649]: AssertionError
juil. 26 20:58:23 raspberrypi systemd[1]: test.service: Main process exited, code=exited, status=1/FAILURE
juil. 26 20:58:23 raspberrypi systemd[1]: test.service: Failed with result 'exit-code'.

I tried with the sounddevice module instead of soundcard and I dont get this error so I guess the problem comes from soundcard and not PulseAudio?

Why all this: I’m making a sound installation, the main python program runs fine on the Raspberry (using multiple sound cards handled by the soundcard module, threading, …), but I struggle to make it start at boot. It seems to run too early with rc.local or a crontab (because sound cards and/or pulseaudio itself are not initialized yet, I guess), so I tried with systemd but I faced the problem described above.

I’m still learning programming and how linux work so sorry if I missed something obvious and if the problem is actually unrelated to soundcard itself.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
arvind-cpcommented, Feb 1, 2022

@ronny-rentner @Teufeuleu : I have the same issue. It works fine on systemd service in ubuntu 18.04 with pulseaudio version 11.1 It fails on systemd service in ubuntu 20.04 pulseaudio version 13.9. soundcard library version is 0.4.1 on both machines. paplay seems to be fine though - both via systemd service as well as command line execution.

Update: After adding ‘sound.target’ as a dependency and sourcing these 2 env variables, it started working in systemd.

Environment="XDG_RUNTIME_DIR=/run/user/1000"
Environment="PULSE_RUNTIME_PATH=/run/user/1000/pulse/"
0reactions
ronny-rentnercommented, Jan 31, 2022

@Teufeuleu: You could try to use pulseaudio’s paplay from your service to see if it actually has access to pulseaudio and can play something. Then you know if the issue is with your pulseaudio setup or with SoundCard.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python import of local module failing when run as systemd ...
The application runs fine when I run it manually. When I run it as a service it fails to find a local module...
Read more >
[SOLVED]Getting systemd to play audio at boot
A problem I encountered is that a sound card does not exist when a service is started. You can check this by inserting...
Read more >
Systemd service not recognizing python library
I found out that the commands are run as root, so I figured it might be that the library hand't been installed on...
Read more >
Commit - rpms/initscripts ... - CentOS Git server
+ - service: filter actions that are not supported by systemctl in service (#947823) ... + - don't throw errors on unreadable /dev/stderr...
Read more >
Bug listing with status RESOLVED with resolution OBSOLETE ...
Bug :1523 - "[IDEA] Offload work by distributing trivial ebuild ... Bug:246091 - "dev-lang/python-2.5.2-r7 - import readline in python writes strange ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found