简体   繁体   中英

Python os.getenv on OSX (Django 1.4)

I just updated an environment to Django 1.4. On syncdb's first run I get the following error:

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

This error is triggered by 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 returns None

After reading this Django ticket, I tried the unofficial patch which worked, however I think I could do better by figuring out what happends..

So I opened a python command line, and tried:

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."

Could I solve this issue within OSX itself? Tips are welcome

The immediate resolution for this, assuming you are using bash as your shell:

$ export LC_ALL=en_US.UTF-8

$ export LANG=en_US.UTF-8

This will set your locale for that session, and syncdb will work. You can add this to your profile and make it permanent for your shells.

You can use the locale command to see the current settings, and locale -a to see what locales are available to you. en_US.UTF-8 is a generic safe one, but you may have other preferences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM