I get a UnicodeDecodeError when running with mod_wsgi

Overview

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.)

error contents

[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.

Countermeasures

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.)

Other

What I checked --ʻImport sys; sys.getdefaultencoding () `check if it is utf-8

Recommended Posts

I get a UnicodeDecodeError when running with mod_wsgi
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
I get a UnicodeDecodeError in mecab-python3
When I get an error with PyInstaller
A memorandum when I tried to get it automatically with selenium
When I get a chromedriver error in Selenium
I get an OS Error: [Errno 8] Exec format error when running a Flask application with a python command
A note I was addicted to when running Python with Visual Studio Code
I get a java.util.regex.PatternSyntaxException when splitting a string in PySpark
I got a UnicodeDecodeError when pip install on ubuntu
A story that stumbled when I made a chatbot with Transformer
I got a Value Error when using JUMAN ++ with PyKNP
I tried to get started with Hy ・ Define a class
I get a KeyError in pyclustering.xmeans
I made a fortune with Python.
I tried running prolog with python 3.8.2.
I made a daemon with Python
[Python] A memo that I tried to get started with asyncio
I wrote a script to get you started with AtCoder fast!
When I get an error with Pylint in Atom on Windows
I get an error when trying to install maec 4.0.1.0 with pip
What to do if you get a UnicodeDecodeError with pip install
A note I was addicted to when creating a table with SQLAlchemy
If you get angry with "too few updates ~" when running LdaModel
I get a can't set attribute when using @property in python
I get an error with import pandas.
I want to get angry with my mom when my memory is tight
Specify options when running flake8 with flycheck
I tried to get started with Hy
I made a character counter with Python
I drew a heatmap with seaborn [Python]
Get a local DynamoDB environment with Docker
I tried a functional language with Python
A workaround when installing pyAudio with pip.
What I did with a Python array
I get a Python No module named'encodings' error with the aws command
A reminder of what I got stuck when starting Atcoder with python
I made a Hex map with Python
I made a life game with Numpy
I made a stamp generator with GAN
I get a firewall warning when I start gqlgen's server.go on my Mac
I made a roguelike game with Python
Be careful when running CakePHP3 with PHP7.2
I made a simple blackjack with Python
I made a configuration file with Python
I made a WEB application with Django
I made a neuron simulator with Python
In the Chainer tutorial, I get an error when importing a package. (mock)
I was in vain because I couldn't get a send parent order with pybitflyer
When I tried to create a virtual environment with Python, it didn't work
I get a strange window when I use the open directory dialog in Tkinter
When I connect to a remote Jupyter Server with VScode, it's remote but local
When I tried to connect with SSH, I got a warning about free space.
If you get a long error when tabbing an interactive shell with Anaconda
A story that I was addicted to when I made SFTP communication with python
I made a stamp substitute bot with line
I got an error when saving with OpenCV
[Python] Get the files in a folder with Python
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
Get a ticket for a theme park with python