pythonutf-8raspberry-pisystemdmpd

Python script controlling mpd with utf-8 charset systemd doesnt work


I've installed MPD on a Raspberry Pi and wrote a Python script to play a playlist when a RFID card was scanned.

My problem is that the script works fine when I call it in the console. It plays every playlist. But when it's started as a service I can only play playlists without äöüß utf8-charset.

My question is now how can i make the script work for utf-8 playlists as a service?

Here my script:

#!/usr/bin/python
# coding=utf-8
import io
import os
import serial
from mpd import MPDClient

with io.open("/var/lib/mpd/mnt/playlists/liste.txt", "r", encoding="utf8") as f:
        list = f.read()
list = list.strip()
list = list.replace("\n","")
list = list[:-1].split(";")

while True:
        ser = serial.Serial("/dev/serial0", baudrate=9600)
        rfid = ser.read(14)

        try:
                tag = { "raw" : rfid,
                        "mfr" : int(rfid[1:5], 16),
                        "id" : int(rfid[5:11], 16),
                        "chk" : int(rfid[11:13], 16)}

                print "Read Card : %s" % tag['id']

                cardnr = str(tag['id'])
                listid = list.index(cardnr)

                print(cardnr)

                plist = list[listid-1]

                print(plist)

                client = MPDClient()
                client.timeout = 10
                client.idletimeout = None
                client.connect("localhost", 6600)
                client.update()

                print "Connected..."

                client.clear()
                print "cleared"
                client.load(plist)
                print(client.playlist())
                print "Playlist geladen"
                client.play(0)
                print "Abspielen"
                client.close()
                client.disconnect()

        except:
                print "error"

systemd service:

[Unit]
Description=Listen to RFID-reader and play mopidy playlists
Requires=mpd.service networking.service
After=mpd.service networking.service

[Service]
Type=idle
Restart=always
ExecStart=/usr/bin/python /var/lib/mpd/mnt/scripts/play.py

[Install]
WantedBy=multi-user.target

output of locale:

LANG=de_DE.UTF-8
LANGUAGE=de_DE.UTF-8
LC_CTYPE="de_DE.UTF-8"
LC_NUMERIC="de_DE.UTF-8"
LC_TIME="de_DE.UTF-8"
LC_COLLATE="de_DE.UTF-8"
LC_MONETARY="de_DE.UTF-8"
LC_MESSAGES="de_DE.UTF-8"
LC_PAPER="de_DE.UTF-8"
LC_NAME="de_DE.UTF-8"
LC_ADDRESS="de_DE.UTF-8"
LC_TELEPHONE="de_DE.UTF-8"
LC_MEASUREMENT="de_DE.UTF-8"
LC_IDENTIFICATION="de_DE.UTF-8"
LC_ALL=

Solution

  • Running the script with ExecStart=/usr/bin/**python3** /var/lib/mpd/mnt/scripts/play.pyhelped.