9

I have just switched from using running a Django App under Python 3 to Using Python 2.7. I now get this error:

SyntaxError: Non-ASCII character '\xe2' in file /Users/user/Documents/workspace/testpro/testpro/apps/common/models/vendor.py on line 9, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

The code its referring to is just a comment:

class Vendor(BaseModel):
    """
    A company manages owns one of more stores.‎
    """
    name = models.CharField(max_length=255)


    def __unicode__(self):
        return self.name

Why?

This works:

 class Vendor(BaseModel):
        """

        """
        name = models.CharField(max_length=255)


        def __unicode__(self):
            return self.name
7
  • 1
    Did you read the linked PEP yet? Commented Jun 9, 2014 at 10:49
  • 1
    You don't have any comments in your posted code. You have a docstring. Commented Jun 9, 2014 at 10:50
  • Please show the actual code that produces that error. The error refers to your source code containing a non-ASCII character, which isn't the case for the snippet of code you posted. Commented Jun 9, 2014 at 10:50
  • @LukasGraf: I am trying to locate a codec that might use some kind of non-breaking space at position E2, but I haven't found one yet. Commented Jun 9, 2014 at 10:51
  • 1
    I just noticed, my bad. @Spike, please ignore my comment. Commented Jun 9, 2014 at 10:53

2 Answers 2

12

You have a UTF-8 encoded U+200E LEFT-TO-RIGHT MARK in your docstring:

'\n    A company manages owns one of more stores.\xe2\x80\x8e\n    '

Either remove that codepoint (and try to use a code editor, not a word processor) from your code, or just put the PEP-263 encoding comment at the top of the file:

# encoding=utf8

Python 3 uses UTF-8 by default, Python 2 defaults to ASCII for source code unless you add that comment.

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

Comments

0

As already pointed out by Martijn Pieters, your docstring contains a UTF-8 (i.e. non-ASCII) character.

I'd like to elaborate a bit, on the proper way to declare file encoding. As it is stated in PEP 263:

To define a source code encoding, a magic comment must be placed into the source files either as first or second line in the file, such as:

# coding=<encoding name>

or (using formats recognized by popular editors):

#!/usr/bin/python
# -*- coding: <encoding name> -*-

or:

#!/usr/bin/python
# vim: set fileencoding=<encoding name> : 

More precisely, the first or second line must match the following regular expression:

^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)

What that means (as it brightly summed up in an answer to another question):

So, you can put pretty much anything before the "coding" part, but stick to "coding" (with no prefix) if you want to be 100% python-docs-recommendation-compatible.

So, for the case, suggested 'magic comments', would be:

# coding=utf8

or:

#!/usr/bin/python
# -*- coding: utf8 -*-

or:

#!/usr/bin/python
# vim: set fileencoding=utf8 :

Comments

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.