3

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your great answer. Some remarks for why I wanted this: I had some trouble in Python with non-ascii characters, so I forced Emacs to use only us-ascii-unix. Only this way I found out that Python has this cool feature of encoding definition too (and I had some has trouble to find out that Python cannot handle the -unix suffix).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.