You are right that Spyder is messing with the encoding in its
sitecustomize.py
<https://code.google.com/p/spyderlib/source/browse/spyderlib/widgets/externalshell/sitecustomize.py>
:
encoding = None
try:
import locale
except ImportError:
pass
else:
loc = locale.getdefaultlocale()
if loc[1]:
encoding = loc[1]
if encoding is None:
encoding = "UTF-8"
try:
sys.setdefaultencoding(encoding)
os.environ['SPYDER_ENCODING'] = encoding
except AttributeError:
# Python 3
pass
If I am so bold as to add encoding = "ascii" then sys.getdefaultencoding()
does indeed give 'ascii' when run from the console, but I don't know what
the consequences will be of messing with the encoding for all of Spyder.
I'd like to change the encoding only for the Python interpreter in the
console, not the Python environment in which Spyder is running.
Unfortunately, adding os.environ['SPYDER_ENCODING'] = 'ascii' doesn't work:
the environment variable stays set but sys.getdefaultencoding() still gives
"UTF-8".
Unfortunately setting the PYTHONIOENCODING environment variable before I
run a Python program from the command line doesn't work either:
$ export PYTHONIOENCODING=UTF-8
$ export | grep PYTHONIOENCODING
declare -x PYTHONIOENCODING="UTF-8"
$ python
Python 2.7.6 |Anaconda 1.8.0 (x86_64)| (default, Jan 10 2014, 11:23:15)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
On Monday, June 16, 2014 12:22:00 AM UTC-4, Adrian Klaver wrote:
>
> On 06/15/2014 03:12 PM, Evan Carter wrote:
> > Thank you for the reply, Adrian. I do not find PYTHONIOENCODING in
> > either environment. Could it be the SPYDER_ENCODING environment variable
> > that's causing the behavior?
> >
> > Running [(k,v) for k, v in os.environ.items() if 'utf' in v.lower() or
> > 'ascii' in v.lower()] in Spyder gives me
> >
> > [('SPYDER_ENCODING', 'UTF-8')]
> >
> > while when I run the same command from a Python shell created from the
> > terminal I get
> >
> > [('LANG', 'en_US.UTF-8')]
> >
>
> I know from the Postgres mailing lists that OS X has some non-standard
> ways of working with locales/encoding. I do not use OS X enough to
> really understand what is going on. I can point to others that have more
> practical experience in this area, so:
>
>
> http://blog.remibergsma.com/2012/07/10/setting-locales-correctly-on-mac-osx-terminal-application/
>
>
> or the suggestions from the first link I sent:
> "
> So typing export PYTHONIOENCODING=utf-8 prior to invoking the Python
> interpreter does the trick, or you could just add this setting to your
> environment file: ~/.MacOSX/environment.plist.
>
> As the other commenters have noted, in Python 2 you can print things of
> class str directly to the terminal (which are bytestrings) but things of
> class unicode need to be converted to bytestrings (str) before they can
> be printed. The easiest way to do this is
> myunicodestring.encode("utf-8") but if you want to be able to say “print
> myunicodestring” without encoding first in the interactive shell, you
> can try putting export LC_CTYPE=en_US.utf-8 in your .bash_profile, just
> so that Python knows that Terminal.app wants its input (Python’s output)
> to be in UTF-8. I think newer versions of Terminal do this automatically
> (10.6), but older ones did not.
> "
>
> Whatever method you choose it would seem you need to give OS X an assist
> in making the correct choice. From the looks of it Anaconda does this by
> setting the SPYDER_ENCODING, my guess in its version of the Python
> startup file.
>
> --
> Adrian Klaver
> [email protected] <javascript:>
>
--
You received this message because you are subscribed to the Google Groups
"spyder" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/spyderlib.
For more options, visit https://groups.google.com/d/optout.