You are not logged in.

#1 2015-12-12 13:52:10

m-den-i
Member
Registered: 2015-12-10
Posts: 15

[SOLVED]Encoding problems with python2 and Django

Recently I have installed Arch linux instead of Ubuntu 15.04. I like it very much but unfortunately I've got an annoying error. I'm fond of web-development and for one of my projects I use Django and python 2 (I really need 2 version and not 3). Sometimes I need to duplicate server's db on my local PC. I do this with Django's ./manage.py dumpdata\loaddata. Using Ubuntu everything was fine but on Arch I get error: UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128). Could anyone help me to solve this problem? Thank you.

Some additional info:
1) As db server i use last pgSql. I checked encodings of databases and they are the same on Ubuntu and Arch (Utf-8)

2) On my Arch I have

➜ locale -a
C
en_US.utf8
POSIX
ru_RU.utf8

3) On server (which is Ubuntu 14.04)

➜ locale -a
C
C.UTF-8
en_US.utf8
POSIX

Last edited by m-den-i (2015-12-14 12:16:00)

Offline

#2 2015-12-13 20:47:22

masterdisaster
Member
Registered: 2013-05-12
Posts: 14

Re: [SOLVED]Encoding problems with python2 and Django

Are you sure you are using Python 2? 'python' executable refers to python3, you may need to use

$ python2 manage.py loaddata fixture_file

Offline

#3 2015-12-14 05:59:24

m-den-i
Member
Registered: 2015-12-10
Posts: 15

Re: [SOLVED]Encoding problems with python2 and Django

Thank you for response, I'm defintely sure that I'm using python 2 (2.7.11) in my virtual environment.

Offline

#4 2015-12-14 09:42:18

masterdisaster
Member
Registered: 2013-05-12
Posts: 14

Re: [SOLVED]Encoding problems with python2 and Django

Allright, try the following on both your local machine and development server and see what you get:

$ ./manage.py shell
>>> import sys
>>> sys.getdefaultencoding()

Chances are 'ascii' and 'utf-8' encoding are mixed up between machines.

Offline

#5 2015-12-14 11:08:21

m-den-i
Member
Registered: 2015-12-10
Posts: 15

Re: [SOLVED]Encoding problems with python2 and Django

Unfortunately, it is not a problem. I've got encoding 'ascii' both on dev server and on local machine. The only difference is between python versions (2.7.6 on dev server and 2.7.11 on local machine).

Offline

#6 2015-12-14 11:56:24

masterdisaster
Member
Registered: 2013-05-12
Posts: 14

Re: [SOLVED]Encoding problems with python2 and Django

Hmm, okay. How about we try to set the encoding manually. Try this on your Arch box

$ ./manage.py shell

>>> import sys
>>> reload(sys)                                        # need to reload sys module to get access to setdefaultencoding() function
>>> sys.setdefaultencoding('utf-8')                    # we change the default encoding
>>> sys.getdefaultencoding()
'utf-8'
>>> from django.core.management import call_command
>>> call_command('loaddata', 'fixture_name')           # where 'fixture_name' is name of you data dump file

If this doesn't work I'm out of ideas. I am afraid this is as far as my 'python h4xor' skills go.

Offline

#7 2015-12-14 12:15:04

m-den-i
Member
Registered: 2015-12-10
Posts: 15

Re: [SOLVED]Encoding problems with python2 and Django

Yes, this solution works, thank you so much, man. smile

Offline

#8 2015-12-14 12:52:30

masterdisaster
Member
Registered: 2013-05-12
Posts: 14

Re: [SOLVED]Encoding problems with python2 and Django

No problem. To make the solution permanent you can create a sitecustomize.py file and set encoding there. For example my virtualenv is located in '~/virtualenvs/python2.7-django'

$ vim ~/virtualenvs/python2.7-django/lib/python2.7/site-packages/sitecustomize.py

and put the following inside

import sys
sys.setdefaultencoding('utf-8')

Afterward you can invoke the Django shell with './manage.py shell' and test if it works.

Offline

Board footer

Powered by FluxBB