在PyCharm中的Django项目上运行sync.db时,getdefaultlocale返回None

Ash*_*Ash 6 python django pycharm

OSX 10.7.3,PyCharm版本2.5构建PY 117.200

我会告诉我如何得到错误:

  1. 我开始一个新项目
  2. 创建一个新的VirtualEnv并选择Python 2.7作为我的基本解释器(不要勾选继承全局包)
  3. 单击Install并选择Django v1.4
  4. 选择Django项目类型
  5. Enable Admin
  6. 打开settings.py并添加sqlite3DATABASES:ENGINE
  7. sync.db
  8. 达到创建数据库超级用户的目的
  9. 区域设置错误

问题似乎是getdefaultlocale()方法没有返回任何东西(好吧,没有).

return getpass.getuser().decode(locale.getdefaultlocale()[1])
    TypeError: decode() argument 1 must be string, not None
Run Code Online (Sandbox Code Playgroud)

但是,如果我启用virtualenv workon MyApplicationpython使用以下代码运行:

Python 2.7.2 (default, Feb  7 2012, 23:16:02) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.10.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> print locale.getdefaultlocale()
('en_GB', 'UTF-8')
Run Code Online (Sandbox Code Playgroud)

那么有人知道发生了什么吗?

全输出:

bash -cl "/Users/ash/.virtualenvs/Tester/bin/python /Applications/PyCharm.app/helpers/pycharm/django_manage.py syncdb"
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Traceback (most recent call last):
  File "/Applications/PyCharm.app/helpers/pycharm/django_manage.py", line 17, in <module>
    run_module(manage_file, None, '__main__')
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 180, in run_module
    fname, loader, pkg_name)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/Users/ash/Development/PycharmProjects/Tester/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/Users/ash/.virtualenvs/Tester/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/ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/Users/ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
    return self.handle_noargs(**options)
  File "/Users/ash/.virtualenvs/Tester/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/ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/core/management/sql.py", line 189, in emit_post_sync_signal
    interactive=interactive, db=db)
  File "/Users/ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 172, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/Users/ash/.virtualenvs/Tester/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/ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/core/management/__init__.py", line 150, in call_command
    return klass.execute(*args, **defaults)
  File "/Users/ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/Users/ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 70, in handle
    default_username = get_default_username()
  File "/Users/ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 105, in get_default_username
    default_username = get_system_username()
  File "/Users/ash/.virtualenvs/Tester/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

Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)

Ash*_*Ash 13

基本上发生这种情况的原因是PyCharm默认情况下不能访问环境变量.

我找到了3个解决这个问题的方法:

  1. 使用CrazyCoder在此问题的评论中建议的链接设置全局环境变量.

  2. 通过更改为PyCharm应用程序目录并运行./pycharm(可能在PATH中创建符号链接以减少不便之处)从终端窗口运行PyCharm

  3. 打开PyCharm首选项,浏览Console > Django Console,输入您所需的环境变量,就像在bash配置文件中一样LC_ALL=en_US.UTF-8

我建议第三种方式最优雅,虽然很烦人,默认情况下不加载变量.

  • 你也可以在你的Django项目的`settings.py`中设置一个相应的环境变量:`import os; os.environ ['LANG'] ='en_US.UTF-8'`.我在Eclipse中遇到了同样的问题,这为我解决了这个问题. (16认同)

Hed*_*ide 7

去终端:

$ nano .bash_profile
Run Code Online (Sandbox Code Playgroud)

加:

export LC_ALL=en_GB.UTF-8
export LANG=en_GB.UTF-8
Run Code Online (Sandbox Code Playgroud)

(或使用locale -a命令查看哪些可用)

保存并重试.