best text editor for programming Python on a Mac

Steven D'Aprano steve at pearwood.info
Fri Jun 17 21:55:15 EDT 2016


On Sat, 18 Jun 2016 09:52 am, Chris wrote:

> I have been trying to write a simple Hello World script on my Mac at work
> with TextEdit.  However, I keep getting this error message:
> 
> SyntaxError: Non-ASCII character '\xe2' in hello_world.py on line 1, but
> no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Have you tried declaring an encoding? Put this in the first line of your
file:

# -*- coding: utf-8 -*-

This is a "magic cookie" that tells the interpreter you are using UTF-8.
It's only needed if you include non-ASCII text in the file, as you appear
to be doing for some reason.

That might be sufficient to solve the problem. In theory it will be, so try
that first, but just in case something more mysterious is going on, read
on.


> I am using TextEdit in plain text mode.  The document was saved in UTF-8,
> and I still get the error message.

Are you *sure* it was UTF-8? Because I can't see likely way to get the byte
\xE2 as the*first* non-ASCII byte in a UTF-8 document.

The way UTF-8 works is that ASCII characters are encoded as the same bytes
used by ASCII. But non-ASCII characters get encoded as multi-byte sequences
of non-ASCII bytes. The first time Python sees a non-ASCII byte, it will
complain. So if it is complaining about byte \xE2 in UTF-8, you must have a
code point between U+10000 and U+10FFFF, which seems unlikely unless you're
writing in Chinese, ancient Phoenician, or similar.

Unless... are you using an emoji? That might do it.

It's best if you show us your code. We may be able to diagnose the problem
more easily once we see that.


If it is possible that you're *not* using UTF-8 like you thought, then
perhaps you have Smart Quotes turned on? If you type ' ' or " ", do you see
curly quotes instead of foot/inch marks?

The character the Python interpreter is complaining about appears to be a
fancy quote of some sort. If I assume you're actually using the old default
Macintosh encoding, I get a kind of curly quote:

py> import unicodedata
py> unicodedata.name(b'\xe2'.decode('MacRoman'))
'SINGLE LOW-9 QUOTATION MARK'

which hints that when you type:

    print 'Hello World'

in your file, you're seeing:

    print ‚Hello World’

or similar. Or possibly you're actually using a Western European encoding,
like Latin-1, in which case you're probably using â.

py> unicodedata.name(b'\xe2'.decode('Latin-1'))
'LATIN SMALL LETTER A WITH CIRCUMFLEX'


But that contradicts the error message that the editor gives you:

> I tried switching to Western ASCII 
> encoding, but once I start typing, I get a message stating that the
> document can no longer be saved using its original Western (ASCII)
> encoding.
> 
> Any suggestions for a good open source text editor for the Mac out there? 
> For now, I am going to stick with vim.

I can only stress that if adding the magic encoding cookie to the file
doesn't fix it, we'll need to see the source code to diagnose the problem.


-- 
Steven




More information about the Python-list mailing list