EPSG31468
EPSG31468

Reputation: 971

Python, Emacs and Encoding

I have trouble with Emacs+Python 2.7.1+Encoding. According to PEP 0263, Python uses the same declaration of source encoding as emacs does.

There is no problem when I start my Python source code script with the following encoding tag:

#!/usr/bin/python
# -*- mode=python; encoding:us-ascii -*-

But when I add a line ending mode to my encoding such as in:

#!/usr/bin/python
# -*- mode=python; encoding:us-ascii-unix -*-

Emacs still acepts my encoding information, but I get the following error from Python when executing my script:

File "./unicode.py", line 2
SyntaxError: encoding problem: with BOM

Is there a way to tell Emacs about the line ending I want to use and at the same time tell Python about the source file encoding?

Upvotes: 3

Views: 834

Answers (1)

Philipp
Philipp

Reputation: 49812

You can write two blocks: one that is parsed only by the interpreter, and one that is only parsed by Emacs:

#!/usr/bin/python
# coding: us-ascii

print "Hello World"    

# Local Variables:
# mode: python
# coding: us-ascii-unix
# End:

Note that (1) us-ascii is the default in Python 2.x; and (2) Emacs is usually able to determine the line ending convention automatically; so you might be able to get along without declaring anything.

Upvotes: 3

Related Questions