python - What exactly does Spyder do to Unicode strings? -
python - What exactly does Spyder do to Unicode strings? -
running python in standard gnu terminal emulator on ubuntu 14.04, expected behavior when typing interactively:
>>> len('tiθ') 4 >>> len(u'tiθ') 3
the same thing happens when running explicitly utf8-encoded script in spyder:
# -*- coding: utf-8 -*- print(len('tiθ')) print(len(u'tiθ'))
...gives next output, regardless of whether run in new dedicated interpreter, or run in spyder-default interpreter (shown here):
>>> runfile('/home/dan/desktop/mwe.py', wdir=r'/home/dan/desktop') 4 3
but when typing interactively in python console within spyder:
>>> len('tiθ') 4 >>> len(u'tiθ') 4
this issue has been brought elsewhere, question regards differences between windows , linux. here, i'm getting different results in different consoles on same system, , python startup message in terminal emulator , in console within spyder identical:
python 2.7.6 (default, mar 22 2014, 22:59:56) [gcc 4.8.2] on linux2 type "help", "copyright", "credits" or "license" more information.
what going on here, , how can python-within-spyder behave python-in-the-shell regard unicode strings? @martijn-pieters makes comment on this question that
spyder sorts of things break normal python environment.
but i'm hoping there's way un-break particular feature, since makes hard debug scripts in ide when can't rely on interactive typed commands yield same results scripts run whole coding: utf-8
declaration.
in gnu terminal:
>>> repr(u'tiθ') "u'ti\\u03b8'" >>> import sys >>> sys.stdin.encoding 'utf-8' >>> sys.getdefaultencoding() 'ascii'
in spyder console:
>>> repr(u'tiθ') "u'ti\\xce\\xb8'" >>> import sys >>> sys.stdin.encoding # returns none >>> sys.getdefaultencoding() 'utf-8'
so knowing that, can convince spyder behave gnu terminal?
after bit of research, seems unusual behavior @ to the lowest degree in part built python (see this give-and-take thread; briefly, python sets sys.stdin.encoding
none
default, , changes if detects host tty
, can observe tty
's encoding).
that said, hackish workaround tell spyder utilize /usr/bin/python3
executable instead of default (which python 2.7.6). when running python 3 console within spyder or within gnu terminal emulator, results different (better!) before, importantly results consistent, regardless of whether running script or typing interactively, , regardless of using gnu terminal or spyder console:
>>> len('tiθ') 3 >>> len(u'tiθ') 3 >>> import sys >>> sys.stdin.encoding 'utf-8' >>> sys.getdefaultencoding() 'utf-8'
this leads other problems within spyder, however: sitecustomize.py
script not python 3 friendly, example, every new interpreter starts with
error in sitecustomize; set pythonverbose traceback: syntaxerror: invalid syntax (sitecustomize.py, line 432)
the interpreter seems work okay anyway, makes me nervous plenty i'm not considering "acceptable" answer, if else has improve idea...
python unicode spyder
Comments
Post a Comment