3

I am looking for a solution to the following bug in a Python 3.7 app engine project (Mac OS Mojave):

ValueError: unknown locale: UTF-8

I have tried all the solutions listed here (edits to my ~/.bash_profile file) without luck.

Could it perhaps be that these proposed fixes do not work because in my case the bug occurs within an app engine environment?

Full traceback:

[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

What seems to be going wrong in the app engine project:

Based on the above traceback, it seems that one library I use (googleads) is trying to get my locale through locale.getdefaultlocale(), resulting in the error.

What makes me suspect this bug is app engine specific:

When I enter the following command in my terminal (using the same python 3.7 as in the app engine environment):

python3 -c 'import locale; print(locale.getdefaultlocale());'

It returns:

('en_US', 'UTF-8')  

In other words, the very same locale.getdefaultlocale() works fine when I call it from terminal, but crashes in the app engine environment.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
Stiño
  • 2,663
  • 7
  • 25
  • 47
  • @snakecharmerb How is this a duplicate? I clearly explain in the question that (1) the solutions in that question do not work for me and that (2) I expect this to be because I run the app in a google app engine environment. The question is if that is indeed the case and what different solution is required to solve my problem. – Stiño May 13 '19 at 11:20
  • @snakecharmerb Unfortunately none of these answers work for me, otherwise I would not ask this question. I have added more info under "What makes me suspect this bug is app engine specific". Can you explain why `locale.getdefaultlocale()` runs fine in terminal but then crashes in app engine? – Stiño May 13 '19 at 14:52
  • Two questions, for the sake of thoroughness: Is the python that you are using to execute the function on the command line the same python (3.7) that is running your app engine application? Is the App Engine app running under the same user? – snakecharmerb May 13 '19 at 15:27
  • @snakecharmerb I executed the command in python 2.7 before but have tested it using python 3.7 and that also works fine. I also updated the question to confirm that I use python 3.7 both on the command line and within the app engine env. I am the only one involved in this project so yes, it's running under the same user. – Stiño May 13 '19 at 15:36
  • Ended up finding a fix and it turns out the bug did somewhat relate to my specific app engine environment: https://stackoverflow.com/a/56130490/3793914 – Stiño May 14 '19 at 12:28
  • Strange that the locale settings aren't inherited from the environment- I wonder if it's a subprocess issue? Anyway, glad you found a workaround. – snakecharmerb May 14 '19 at 12:54

0 Answers0