久々の記事です。
今まで備忘録をサボっていましたが、やはり都度記録に残すことが大事だと思いなおし、Djangoの勉強がてら、躓いた小さな石から大きな石まで記事に残そうと思います。
というわけで、1年前につくったアプリを久々に触ろうと思いDBコンテナを新しく作り直したため、管理者ユーザも新規で作り直そうとしたところでこけました。
Pythonいじってると必ずといっていいほど出てくる「UnicodeEncodeError」ですね。
# python manage.py createsuperuser Traceback (most recent call last): File "manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "/usr/local/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line utility.execute() File "/usr/local/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/python/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv self.execute(*args, **cmd_options) File "/usr/local/python/lib/python3.6/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 62, in execute return super().execute(*args, **options) File "/usr/local/python/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute output = self.handle(*args, **options) File "/usr/local/python/lib/python3.6/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 115, in handle username = self.get_input_data(self.username_field, input_msg, default_username) File "/usr/local/python/lib/python3.6/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 196, in get_input_data raw_value = input(message) UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)
このエラー自体は、マルチバイト文字をUnicodeでエンコードしようとして失敗した場合に起きるっぽいです。
ただ、createsuperuserで起きてしまっているので、ソースの中に原因があるというよりは外(環境面)に原因がありそうです・・・。
と調べていたら以下の記事が出てきました。
fmfm、PYTHONIOENCODINGという環境変数があるのですね。
全然知りませんでした(笑)
# export PYTHONIOENCODING="UTF-8" # python manage.py createsuperuser ユーザー名 (leave blank to use 'root'):
無事に応答が返ってきました。
PYTHONIOENCODINGというくらいなので、createsuperuser以外でも使われそうな感じしますよね。
毎回設定するのも面倒くさいので、profileに書いとくかDockerfileに書いとくのがよいかもしれません。