[PYTHON] I was addicted to deploying GoogleCloudFunctions ('ascii' codec can't encode character u'\ u281b' in position 58 appears)

Overview

--When I tried to deploy GCP Cloud Functions locally, it didn't work. ――After trial and error, it went well, so make a note of the verification process and solution. ――I hope it helps those who are addicted to similar errors.

error contents

[usr_id@PC_NAME] ~/Documents/project/function_name
% gcloud functions deploy function-name --region asia-northeast1 --runtime python37 --trigger-topic topic-name         #Deploy command
Deploying function (may take a while - up to 2 minutes)...failed.                                                                                                                                                                                                         
ERROR: gcloud crashed (UnicodeEncodeError): 'ascii' codec can't encode character u'\u281b' in position 58: ordinal not in range(128)
If you would like to report this issue, please run the following command:
  gcloud feedback
To check gcloud for common problems, please run the following command:
  gcloud info --run-diagnostics
[usr_id@PC_NAME] ~/Documents/project/function_name
% gcloud info --run-diagnostics         #Error details
Network diagnostic detects and fixes local network connection issues.
Exception in thread Thread-1:                                                                                                                                                                                                                                             
Traceback (most recent call last):
  File "/Users/usr_id/.pyenv/versions/2.7.0/lib/python2.7/threading.py", line 530, in __bootstrap_inner
    self.run()
  File "/Users/usr_id/.pyenv/versions/2.7.0/lib/python2.7/threading.py", line 483, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/Users/usr_id/google-cloud-sdk/lib/googlecloudsdk/core/console/progress_tracker.py", line 164, in Ticker
    if self.Tick():
  File "/Users/usr_id/google-cloud-sdk/lib/googlecloudsdk/core/console/progress_tracker.py", line 235, in Tick
    self._Print(self._GetSuffix())
  File "/Users/usr_id/google-cloud-sdk/lib/googlecloudsdk/core/console/progress_tracker.py", line 261, in _Print
    self._console_output.UpdateConsole()
  File "/Users/usr_id/google-cloud-sdk/lib/googlecloudsdk/core/console/multiline.py", line 154, in UpdateConsole
    self._UpdateConsole()
  File "/Users/usr_id/google-cloud-sdk/lib/googlecloudsdk/core/console/multiline.py", line 168, in _UpdateConsole
    self._messages[self._last_print_index].Print()
  File "/Users/usr_id/google-cloud-sdk/lib/googlecloudsdk/core/console/multiline.py", line 281, in Print
    self._WriteLine(line)
  File "/Users/usr_id/google-cloud-sdk/lib/googlecloudsdk/core/console/multiline.py", line 331, in _WriteLine
    self._stream.write(self._level * INDENTATION_WIDTH * ' ' + line)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u281b' in position 30: ordinal not in range(128)
Checking network connection...done.                                                                                                                                                                                                                                       
Reachability Check passed.
Network diagnostic passed (1/1 checks passed).
Property diagnostic detects issues that may be caused by properties.
Checking hidden properties...done.                                                                                                                                                                                                                                        
Hidden Property Check passed.
Property diagnostic passed (1/1 checks passed).

--I get an unclear error when deploying

gcloud crashed (UnicodeEncodeError): 'ascii' codec can't encode character u'\u281b' in position 58: ordinal not in range(128)

--Looking at the log, it seems that the deployment went well and failed when returning the result to the terminal. --Looking at the console, the deployment is successful --Probably an error is occurring while trying to return the result to the terminal

Settings

--Python used in SDK --python2.7 put in pyenv

[usr_id@PC_NAME] ~/Documents/project/function-name
% echo $CLOUDSDK_PYTHON
/Users/usr_id/.pyenv/versions/2.7.0/bin/python

Correspondence 1

--Try changing the default encoding --Reference:

Before changing the default encoding

% $CLOUDSDK_PYTHON
Python 2.7 (r27:82500, Nov  1 2019, 14:21:44)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'shift-jis'
>>>

Try changing the default encoding

[usr_id@PC_NAME] ~/.pyenv/versions/2.7.0/lib/python2.7/site-packages
% pwd
/Users/usr_id/.pyenv/versions/2.7.0/lib/python2.7/site-packages
[usr_id@PC_NAME] ~/.pyenv/versions/2.7.0/lib/python2.7/site-packages
% cat sitecustomize.py
import sys
sys.setdefaultencoding('utf-8')
[usr_id@PC_NAME] ~/.pyenv/versions/2.7.0/lib/python2.7/site-packages
% $CLOUDSDK_PYTHON
Python 2.7 (r27:82500, Nov  1 2019, 14:21:44)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'utf-8'
>>>

--It was no good --In the first place, the default encoding is shift-jis, which does not match the error content'ascii' codec can't encode. --Maybe you specified the python that you put in pyenv with $ CLOUDSDK_PYTHON, but the python that is actually used in the deployment is different ...?

Correspondence 2

--Try changing the default encoding with the Mac default Python → ** This is the solution! ** **

Try to change

[usr_id@PC_NAME] /Library/Python/2.7/site-packages
% echo $CLOUDSDK_PYTHON
/usr/bin/python2.7
[usr_id@PC_NAME] /Library/Python/2.7/site-packages
% pwd
/Library/Python/2.7/site-packages
[usr_id@PC_NAME] /Library/Python/2.7/site-packages
% $CLOUDSDK_PYTHON
Python 2.7.16 (default, Oct 16 2019, 00:34:56)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'utf-8'
>>>

――This solved it!

[usr_id@PC_NAME] ~/Documents/project/function-name
% gcloud functions deploy function-name --region asia-northeast1 --runtime python37 --trigger-topic topicname
Deploying function (may take a while - up to 2 minutes)...done.                                                                                                                                       
availableMemoryMb: 256
entryPoint: function-name
eventTrigger:
  eventType: google.pubsub.topic.publish
  failurePolicy: {}
  resource: projects/project-name/topics/topicname
  service: pubsub.googleapis.com
ingressSettings: ALLOW_ALL
labels:
  deployment-tool: cli-gcloud
name: projects/project-name/locations/asia-northeast1/functions/function-name
runtime: python37
serviceAccountEmail: [email protected]
sourceUploadUrl: hogehoge
status: ACTIVE
timeout: 60s
updateTime: '2020-02-06T02:15:16Z'
versionId: '19'

Cause (imagination)

--It seems that it cannot be displayed if the symbol output at the indicator after executing cli is ascii. --ʻU'\ u281b'` → ⠛ It was this symbol ―― ↓

Remarks

--I forgot to write down the version of the gcloud command (because the environment where I wrote the Qiita article is different from the environment I was addicted to), so I will add it if I remember it later

Recommended Posts

I was addicted to deploying GoogleCloudFunctions ('ascii' codec can't encode character u'\ u281b' in position 58 appears)
'shift_jis' codec can't encode character'\ uff0d' in position 0: resolution of illegal multibyte sequence
I was addicted to scraping with Selenium (+ Python) in 2020
I was addicted to trying logging.getLogger in Flask 1.1.x
'Ascii' codec can't encode characters in position xx-xx: ordinal not in range (128) when calling PHP → Python
What I was addicted to with json.dumps in Python base64 encoding
I was addicted to confusing class variables and instance variables in Python
I was addicted to multiprocessing + psycopg2
The file name was bad in Python and I was addicted to import
I was addicted to pip install mysqlclient
I was addicted to Flask on dotCloud
What I was addicted to Python autorun