Forums

"sitecustomize.py" (of the Django) not working

Hi.

I made a web application with django1.8, and python2.7 (in virtualenv).

And I want to use Korean(because I'm Korean...) in my web app.

Then, I have to change the Django's default encoding to utf-8

(Unless it shows an error "UnicodeEncodeError at /admin/blog/post/add/ 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)")

So I made a "sitecustomize.py" file with setting "sys.setdefaultencoding('utf-8')" in it. And I placed it to '~myapp/myvenv/lib/python2.7/site-packages'.

But it does not work. Still it says the Django's default encoding is "ascii".

It works perfectly in my local computer, but not in Pythonanywhere.

Need help please. Thank you.

This sounds like it's being caused by a difference in the sys.path between the two environments. What do you see in the server and error logs if you put this into your settings.py:

import sys
import sitecustomize
print > sys.stderr, "sitecustomize is being loaded from %s" % (sitecustomize.__file__,)

...?

Thank you for helping, giles.

My "Server log" says "sitecustomize is being loaded from /usr/lib/python2.7/sitecustomize.pyc".

I think it's a kind of "global" sitecustomize.

Any way to override it?

Thank you.

You could try re-arranging the sys.path in your WSGI file. But if you're just trying to set the character set, perhaps the best option is to bypass sitecustomize.py completely and put the code you're putting there in the WSGI file instead, near the top before you import any Django-related stuff.

Solved!

When I checked my python path, "~myapp/myvenv/lib/python2.7" was in the very first place. So I just moved my sitecustomize.py file to there (rather than "~myapp/myvenv/lib/python2.7/site-packages") and loaded well.

I learned a lot about paths. Thank you.