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
Prometheus
  • 32,405
  • 54
  • 166
  • 302

2 Answers2

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
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 :