0

I have a script managing mpc-commands with python3. I use subprocess.check_output to get information:

s = (subprocess.check_output("mpc current -f %title%", shell=True,encoding = 'utf-8'))

That works well and gives me the value as a string.

Trying to install a service for the script fails with the error:

File "/usr/lib/python2.7/subprocess.py", line 216, in check_output
 process = Popen(stdout=PIPE, *popenargs, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'encoding'

Same result (working script, failing service) with 'text = true' instead of 'encoding = 'utf8''.

Why is python2.7 used for subprocess instead of python3? In the seccond line of the script I have

# -*- coding: utf-8 -*-

Version of Python is 3.7.3

heimi
  • 499
  • 7
  • 16
  • Apparently you are using Python 2.7. What's the output of `import sys; print(sys.version_info)` and `print(sys.executable)`? – a_guest Mar 11 '20 at 10:05
  • sys.version_info(major=3, minor=7, micro=3, releaselevel='final', serial=0) shebang is #!/usr/bin/python3 – heimi Mar 11 '20 at 10:18
  • sys_executable: /usr/bin/python3 – heimi Mar 11 '20 at 10:19
  • What do you mean by *"Trying to install a service for the script"*? Did you check these values inside the script that produces the error? – a_guest Mar 11 '20 at 10:20
  • I installed a service and run systemctl start mpctask.service. Then I run systemctl status mpctask.service with the error above. - I got it to run by using universal_newlines=True instead of encoding=utf8. But why python2.7 is used for the subprocess? The system is updated, and subprocess.py is in present in /usr/lib/python3.7 – heimi Mar 11 '20 at 10:28
  • Can you add the code for the service (i.e. `cat mpctask.service`)? Can you add the logging information of `sys.*` to the script run by the service as well and then check via `journalctl`? I suppose the service is run on Python 2.7 instead of Python 3. – a_guest Mar 11 '20 at 10:31
  • I overlooked that in the service task I wrote the ExecStart sequence with python instead of python3: 'ExecStart=/usr/bin/python3 /home/pi/mpctask.py'. It is working now as expected and I hope that this was the mistake. Thanks for your advice, a_guest – heimi Mar 13 '20 at 19:11

0 Answers0