-1

Consider the following script :

#!/usr/bin/env python3

print("<h1>é</h1>")

I am trying to run it from PHP :

<?php
    system("/usr/bin/python3 /my/script.py");
?>

It works fine from shell, but crashes through PHP :

UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 3: ordinal not in range(128)

And if I add .encode('utf-8') PHP prints the byte encoded string :

b'\xc3\xa9'

How can I fix it ? Adding # -*- coding: utf-8 -*- to the python script or prefixing the string with u didn't help, and I thought utf-8 was supposed to be the default for python3 anyway ?

Skippy le Grand Gourou
  • 6,976
  • 4
  • 60
  • 76

1 Answers1

1

A fix is to set PYTHONIOENCODING explicitely :

<?php
    system("PYTHONIOENCODING=utf-8 /usr/bin/python3 /my/script.py");
?>

[Credits.]

Skippy le Grand Gourou
  • 6,976
  • 4
  • 60
  • 76