Trouble running Python code to read a file. When running from Apache with mod_wsgi, it worked normally in the terminal, but when running with Apache, I encountered ʻUnicodeDecodeError`. As a result, setting the locale was a problem. I'm a little addicted to it, so I'll leave it as a memorandum. (Since it runs on Ubuntu Apache, the directory structure etc. will be slightly different if it is a RHEL system.)
[Thu Jul 06 10:50:28.381851 2017] [wsgi:error] [pid 6935] [hogehoge] for line in fin:
[Thu Jul 06 10:50:28.381858 2017] [wsgi:error] [pid 6935] [hogehoge] File "/usr/path/to/python/encodings/ascii.py", line 26, i
n decode
[Thu Jul 06 10:50:28.381862 2017] [wsgi:error] [pid 6935] [hogehoge] return codecs.ascii_decode(input, self.errors)[0]
[Thu Jul 06 10:50:28.381882 2017] [wsgi:error] [pid 6935] [hogehoge] UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 0: ordinal not i
n range(128)
In this way, as for line in fin
, which is often done in Python, an error occurs just by trying to read a file in which Japanese is written.
Since the error is ʻUnicodeDecodeError`, you can see that it seems to be moss around the character code.
It was due to Apache settings.
The system is LANG =" en_US.UTF-8 "
, which is applied when debugging in the terminal.
When running on Apache, the environment variable settings described in / etc / apache2 / envvars
are applied.
In this case, ʻexport LANG = C was set here. If you align this with the above and set ʻexport LANG =" en_US.UTF-8 "
, it works (the comment says that ʻexport LANG goes to read
/ etc / default / locale`. Did not work well.)
What I checked --ʻImport sys; sys.getdefaultencoding () `check if it is utf-8
Recommended Posts