Django 1.4のsyncdbでエラー
Django 1.4でsyncdbして、管理ユーザーを作成するところで以下のエラーが発生しました。
環境はmacosx 10.7.4。
Traceback (most recent call last):
File "/Applications/PyCharm.app/helpers/pycharm/django_manage.py", line 20, in <module>
run_module(manage_file, None, '__main__', True)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/Users/yuhei/Dropbox/workspace/django_sample/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/yuhei/.virtualenvs/django14/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/Users/yuhei/.virtualenvs/django14/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/yuhei/.virtualenvs/django14/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/yuhei/.virtualenvs/django14/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/Users/yuhei/.virtualenvs/django14/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/Users/yuhei/.virtualenvs/django14/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 110, in handle_noargs
emit_post_sync_signal(created_models, verbosity, interactive, db)
File "/Users/yuhei/.virtualenvs/django14/lib/python2.7/site-packages/django/core/management/sql.py", line 189, in emit_post_sync_signal
interactive=interactive, db=db)
File "/Users/yuhei/.virtualenvs/django14/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 172, in send
response = receiver(signal=self, sender=sender, **named)
File "/Users/yuhei/.virtualenvs/django14/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 73, in create_superuser
call_command("createsuperuser", interactive=True, database=db)
File "/Users/yuhei/.virtualenvs/django14/lib/python2.7/site-packages/django/core/management/__init__.py", line 150, in call_command
return klass.execute(*args, **defaults)
File "/Users/yuhei/.virtualenvs/django14/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/Users/yuhei/.virtualenvs/django14/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 70, in handle
default_username = get_default_username()
File "/Users/yuhei/.virtualenvs/django14/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 105, in get_default_username
default_username = get_system_username()
File "/Users/yuhei/.virtualenvs/django14/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 85, in get_system_username
return getpass.getuser().decode(locale.getdefaultlocale()[1])
TypeError: decode() argument 1 must be string, not None
既知の不具合のようで、こちらで報告されています
https://code.djangoproject.com/ticket/16017
チケットによると、この不具合は以下のコードでデフォルトロケールが取得できない場合に発生します
import locale locale.getdefaultlocale() #=> (None, None)
コメントにある通り、環境変数LANGを設定することで回避できました
export LANG='ja_JP.UTF-8'
locale.getdefaultlocale()は設定した値を返すようになります
import locale locale.getdefaultlocale() #=> ('ja_JP', 'UTF-8')