title: How to fix `ValueError: unknown locale: UTF-8` in app engine project?
body: <p>I am looking for a solution to the following bug in a Python 3.7 app engine project (Mac OS Mojave):</p>
  
  <blockquote>
    <p>ValueError: unknown locale: UTF-8</p>
  </blockquote>
  
  <p>I have tried all the solutions listed <a href="https://stackoverflow.com/questions/19961239/pelican-3-3-pelican-quickstart-error-valueerror-unknown-locale-utf-8">here</a> (edits to my <code>~/.bash_profile</code> file) without luck. </p>
  
  <p>Could it perhaps be that these proposed fixes do not work because in my case the bug occurs within an app engine environment?</p>
  
  <p><strong>Full traceback:</strong></p>
  
  <pre><code>[2019-05-13 07:48:37,563] ERROR in app: Exception on /api/get_accounts [POST]
  Traceback (most recent call last):
    File "/private/var/folders/jj/9jn8lbt17kgfhr_dqdz6gd180000gn/T/tmpXEC0bT/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
      response = self.full_dispatch_request()
    File "/private/var/folders/jj/9jn8lbt17kgfhr_dqdz6gd180000gn/T/tmpXEC0bT/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
      rv = self.handle_user_exception(e)
    File "/private/var/folders/jj/9jn8lbt17kgfhr_dqdz6gd180000gn/T/tmpXEC0bT/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
      return cors_after_request(app.make_response(f(*args, **kwargs)))
    File "/private/var/folders/jj/9jn8lbt17kgfhr_dqdz6gd180000gn/T/tmpXEC0bT/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
      reraise(exc_type, exc_value, tb)
    File "/private/var/folders/jj/9jn8lbt17kgfhr_dqdz6gd180000gn/T/tmpXEC0bT/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
      raise value
    File "/private/var/folders/jj/9jn8lbt17kgfhr_dqdz6gd180000gn/T/tmpXEC0bT/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
      rv = self.dispatch_request()
    File "/private/var/folders/jj/9jn8lbt17kgfhr_dqdz6gd180000gn/T/tmpXEC0bT/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
      return self.view_functions[rule.endpoint](**req.view_args)
    File "/Users/.../googleAdsApi/main.py", line 61, in get_all_accounts
      accounts = get_accounts(creds)
    File "/Users/.../googleAdsApi/services/get_accounts.py", line 7, in get_accounts
      client = get_client(creds)
    File "/Users/.../googleAdsApi/services/clients.py", line 58, in get_client
      return adwords.AdWordsClient(DEVELOPER_TOKEN, oauth2_client, USER_AGENT, cache=ZeepServiceProxy.NO_CACHE)
    File "/private/var/folders/jj/9jn8lbt17kgfhr_dqdz6gd180000gn/T/tmpXEC0bT/lib/python3.7/site-packages/googleads/adwords.py", line 241, in __init__
      super(AdWordsClient, self).__init__()
    File "/private/var/folders/jj/9jn8lbt17kgfhr_dqdz6gd180000gn/T/tmpXEC0bT/lib/python3.7/site-packages/googleads/common.py", line 152, in __init__
      _, encoding = locale.getdefaultlocale()
    File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/locale.py", line 568, in getdefaultlocale
      return _parse_localename(localename)
    File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/locale.py", line 495, in _parse_localename
      raise ValueError('unknown locale: %s' % localename)
  ValueError: unknown locale: UTF-8
  INFO     2019-05-13 06:48:37,661 module.py:861] default: "POST /api/get_accounts HTTP/1.1" 500 290
  </code></pre>
  
  <p><strong>What seems to be going wrong in the app engine project:</strong></p>
  
  <p>Based on the above traceback, it seems that one library I use (<a href="https://pypi.org/project/googleads/" rel="nofollow noreferrer">googleads</a>) is trying to get my locale through <code>locale.getdefaultlocale()</code>, resulting in the error. </p>
  
  <p><strong>What makes me suspect this bug is app engine specific:</strong></p>
  
  <p>When I enter the following command in my terminal (using the same python 3.7 as in the app engine environment):</p>
  
  <pre><code>python3 -c 'import locale; print(locale.getdefaultlocale());'
  </code></pre>
  
  <p>It returns:</p>
  
  <pre><code>('en_US', 'UTF-8')  
  </code></pre>
  
  <p>In other words, the very same <code>locale.getdefaultlocale()</code> works fine when I call it from terminal, but crashes in the app engine environment. </p>