OSX 上的 Python os.getenv (Django 1.4)

标签 python django linux macos

我刚刚将环境更新到 Django 1.4。在 syncdb 的第一次运行中,我收到以下错误:

TypeError: decode() argument 1 must be string, not None

此错误由django/contrib/auth/management/init触发:

try:
    return getpass.getuser().decode(locale.getdefaultlocale()[1])
except (ImportError, KeyError, UnicodeDecodeError):
    # KeyError will be raised by os.getpwuid() (called by getuser())
    # if there is no corresponding entry in the /etc/passwd file
    # (a very restricted chroot environment, for example).
    # UnicodeDecodeError - preventive treatment for non-latin Windows.
    return u''

getdefaultlocale 返回 None

看完this Django 票,我尝试了有效的非官方补丁,但是我认为我可以通过弄清楚发生了什么来做得更好..

所以我打开了一个 python 命令行,并尝试:

import os
print os.getenv()
None
os.getenv.__doc__
"Get an environment variable, return None if it doesn't exist.\n    The optional second argument can specify an alternate default."

我可以在 OSX 本身解决这个问题吗?欢迎提示

最佳答案

假设您使用 bash 作为 shell,立即解决这个问题:

$ export LC_ALL=en_US.UTF-8

$ export LANG=en_US.UTF-8

这将为该 session 设置您的语言环境,syncdb 将起作用。您可以将其添加到您的个人资料中,并使其永久适用于您的 shell。

您可以使用 locale 命令查看当前设置,并使用 locale -a 查看可用的语言环境。 en_US.UTF-8 是一种通用的安全编码,但您可能有其他偏好。

关于OSX 上的 Python os.getenv (Django 1.4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9886178/

相关文章:

python类属性继承

python - 在 Django 中发送群发邮件——太多的值无法解压

django - 如何选择多对多关系中未引用的所有对象

c - 从 GSocket 接收垃圾

python - 解释 Numpy-Array 中的特定位

python - 通过 ssh 可以替代 "python3 sample_program.py &"的方法是什么?

python - Windows 上的异步子进程

Django、ModelForms、User 和 UserProfile - 不散列密码

c - 在使用之前对结构进行 memset 是一种很好的风格吗?

linux - 是否可以在信号处理程序中设置计时器?