Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python SyntaxError: Non-ASCII character '\xe2' in file

Tags:

python

django

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
like image 998
Prometheus Avatar asked Jun 09 '14 10:06

Prometheus


1 Answers

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.

like image 62
Martijn Pieters Avatar answered Nov 17 '22 13:11

Martijn Pieters