0

I have a very simple python command

python3 -c "print(b'\xe9'.decode('iso-8859-1'))"

When I run it from the command prompt it works fine. However, if I wrap it in a service and place it in /etc/init/test_service like this

description "test of iso-8859-1 issue"

start on runlevel [2345]
stop on runlevel [!2345]

setuid www-data
setgid www-data


script
        exec python3 -c "print(b'\xe9'.decode('iso-8859-1'))"
end script

Then it fails with error

Traceback (most recent call last):
  File "<string>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 0: ordinal not in range(128)

Most of the other answers I've found don't assume you need to install something to make iso-8859-1 available.

James Robinson
  • 1,222
  • 1
  • 15
  • 43
  • Presumably the locale etc when running as root is different. Try the usual workarounds to set an explicit encoding for standard output. – tripleee Jan 17 '17 at 13:17
  • Possible duplicate of [Changing default encoding of Python?](http://stackoverflow.com/questions/2276200/changing-default-encoding-of-python) – tripleee Jan 17 '17 at 13:27
  • Interesting the issue is with the printing to the command line. – James Robinson Jan 17 '17 at 13:30
  • `PYTHONIOENCODING=utf8 python3 -c "print(b'\xe9'.decode('iso-8859-1'))"` works for me, but without knowledge of your locale, it's unclear whether the result I get is the one you expect. – tripleee Jan 17 '17 at 13:33
  • Does it fail if you dont have the PYTHONIOENCODING=utf8 – James Robinson Jan 17 '17 at 13:40
  • Yes, it does. Docker mint Ubuntu image (modulo `apt-get update; apt-get install -y python3`) running as root. – tripleee Jan 17 '17 at 13:42

0 Answers0