UnicodeEncodeError on Django uploaded files

This has been an error that’s been bugging me for months. For some reason in production settings only in the production server only, whenever a user uploaded a files with a non ascii filename, I’d get this error

UnicodeEncodeError: 'ascii' codec can't encode character u'\xed' in position 54: ordinal not in range(128)

And I was unable to reproduce.

Looking at the last line of the trace, it would be os.stat that would break, and none of my code would be involved, nor the database driver so it seems to be a file system issue.

The system’s locale:

LANG=en_US.utf8
LC_CTYPE="en_US.utf8"
LC_NUMERIC="en_US.utf8"
LC_TIME="en_US.utf8"
LC_COLLATE="en_US.utf8"
LC_MONETARY="en_US.utf8"
LC_MESSAGES="en_US.utf8"
LC_PAPER="en_US.utf8"
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT="en_US.utf8"
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=

After a lot of wandering around, I ended up on Django’s documentation in the Apache Additional tweaking section, even though my application is running under fastCGI. and I foudn this:

export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'

It turns out that LC_ALL does not have a value. I added those two lines to my fcgi launching script, and problem solved!