当在PyCharm中的Django项目上运行sync.db时,getdefaultlocale返回None [英] getdefaultlocale returning None when running sync.db on Django project in PyCharm

查看:259
本文介绍了当在PyCharm中的Django项目上运行sync.db时,getdefaultlocale返回None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OSX 10.7.3,PyCharm版本2.5构建PY 117.200



我将介绍如何得到错误:


  1. 我开始一个新项目

  2. 创建一个新的VirtualEnv,并选择Python 2.7作为我的基本解释器(保留继承全局包未勾选) / li>
  3. 单击安装并选择Django v1.4

  4. 选择 Django 项目类型

  5. 勾选启用管理

  6. 打开 settings.py 并将 sqlite3 添加到 DATABASES:ENGINE

  7. 运行 sync.db

  8. 创建数据库超级用户

  9. 区域设置错误

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



$ {code} return getpass.getuser()。decode(locale.getdefaultlocale()[1])$ ​​b $ b TypeError:decode()参数1必须是字符串,而不是$ $ $ $ b

但是如果我en可以使用以下代码来运行virtualenv workon MyApplication 并运行 python


$ b $苹果公司(Apple Clang)3.0(标签/苹果/ cl ang ang ang-------------------------------------- 211.10.1)]在darwin
有关更多信息,请输入help,copyright,credits或license。
>>>导入区域设置
>>>打印locale.getdefaultlocale()
('en_GB','UTF-8')



完整输出:



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

刚刚安装了Django的验证系统,这意味着您没有定义任何超级用户。
您要立即创建一个? (是/否):是
追溯(最近的最后一次呼叫):
文件/Applications/PyCharm.app/helpers/pycharm/django_manage.py,第17行在< module>
run_module(manage_file,None,'__main__')
文件/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py,第180行,在run_module
fname,loader,pkg_name)
文件/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py,第72行,在_run_code
exec_globals中的exec代码
文件/Users/ash/Development/PycharmProjects/Tester/manage.py,第10行在< module>
execute_from_command_line(sys.argv)
文件/Users/ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/core/management/__init__.py,第443行,在execute_from_command_line
utility.execute()
文件/Users/ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/core/management/__init__.py,第382行,执行
self.fetch_command(子命令).run_from_argv(self.argv)
文件/Users/ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/core/ management / base.py,第196行,在run_from_argv
self.execute(* args,** options .__ dict__)
文件/Users/ash/.virtualenvs/Tester/lib/python2.7 /site-packages/django/core/management/base.py,第232行,执行
output = self.handle(* args,** options)
文件/ Users / ash /。 $ //
return self.handle_noargs(** options)
文件/// lib / python2.7 / site-packages / django /用户/灰/ .virtualenvs /测试/ lib中/ python2.7 /秒ite-packages / django / core / management / commands / syncdb.py,第110行,在handle_noargs
emit_post_sync_signal(created_models,verbosity,interactive,db)
文件/Users/ash/.virtualenvs/ Tester / lib / python2.7 / site-packages / django / core / management / sql.py,第189行,emit_post_sync_signal
interactive = interactive,db = db)
文件/ Users / ash /.virtualenvs/Tester/lib/python2.7/site-packages/django/dispatch/dispatcher.py,第172行,发送
response = receiver(signal = self,sender = sender,** named)
文件/Users/ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py,第73行,在create_superuser
call_command( creationuperuser,interactive = True,database = db)
文件/Users/ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/core/management/__init__.py,行150,在call_command
return klass.execute(* args,** defaults)
文件/Users/ash/.virtualenvs/Tester/lib/python2.7/site-packages /django/core/management/base.py,第232行,执行
output = self.handle(* args,** options)
文件/Users/ash/.virtualenvs/Tester/ lib / python2.7 / site-packages / django / contrib / auth / management / commands / creationuperuser.py,第70行,在句柄
default_username = get_default_username()
文件/ Users / ash / .virtualenvs / Tester / lib / python2.7 / site-packages / django / contrib / auth / management / __ init__.py,第105行,get_default_username
default_username = get_system_username()
文件/ Users /ash/.virtualenvs/Tester/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py,第85行,get_system_username
return getpass.getuser()。decode(locale .getdefaultlocale()[1])$ ​​b $ b TypeError:decode()参数1必须是字符串,而不是无

处理完成退出代码1


解决方案

基本上,发生这种情况的原因是PyCharm默认情况下不能访问environmenta l变量



我已经找到3个解决方案:



  1. 通过更改为PyCharm从终端窗口运行PyCharm,设置全局环境变量,使用CrazyCoder链接建议的链接。应用程序目录并运行 ./ pycharm (可能在您的PATH中创建一个符号链接,以减少不便)


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


建议第三种方式是最优雅的,尽管默认情况下变量不加载是令人讨厌的。


OSX 10.7.3, PyCharm version 2.5 build PY 117.200

I'll run through how I get the error:

  1. I start a new project
  2. Create a new VirtualEnv and select Python 2.7 as my base interpreter (leave inherit global packages un-ticked)
  3. Click Install and choose Django v1.4
  4. Select Django project type
  5. Tick Enable Admin
  6. Open settings.py and add sqlite3 to DATABASES:ENGINE
  7. Run sync.db
  8. Get to the point of creating a database superuser
  9. locale error

The problem seems to be that getdefaultlocale() method is not returning anything (well, None).

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

However if I enable the virtualenv workon MyApplication and run python with the following code:

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')

So does anybody know what's going on?

Full Output:

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

解决方案

Basically the reason this occurs is that PyCharm doesn't, by default, have access to environmental variables.

I've found 3 solutions to this problem:

  1. Set a global environmental variable, using the link CrazyCoder suggests in the comments to this question.

  2. Run PyCharm from a terminal window by changing to the PyCharm application directory and running ./pycharm (possibly create a symlink in your PATH to make this less inconvenient)

  3. Open PyCharm preferences, browse to Console > Django Console, type in your required environmental variables as you would in your bash profile e.g. LC_ALL=en_US.UTF-8

I'd suggest the 3rd way as most elegant, although it's annoying that variables aren't loaded by default.

这篇关于当在PyCharm中的Django项目上运行sync.db时,getdefaultlocale返回None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆