Search code examples
phplinuxbashpython-3.xsystem

Using UTF-8 for PHP System call to Python


A system call exists in PHP such that

system("python3 returnUTF8.py")

If I run the Python script on the Linux command line - it works fine, however when I use the PHP system command I get an error "UnicodeEncodingError - can't encode to ascii"...

I understand the problem to be that the print function in Python defaults to the result the caller wants, and this is shown when I run print (sys.stdout.encoding) on the command line I get UTF-8 and from the system call I get ANSI_X3.4-1960.

How can I change the system call in PHP or the Python script to use UTF-8 encoding?


Solution

  • You'll need to tell Python what encoding to use; normally this is taken from your terminal locale, but when you use system() there is no such locale and then the default C locale encoding is used, which is ANSI_X3.4-1960 (aka ASCII).

    Set the PYTHONIOENCODING environment variable:

    system("PYTHONIOENCODING=utf-8 python3 returnUTF8.py")
    

    or explicitly encode to UTF-8 in your program.