1

I have some accented characters in my Python source code (comments, strings), and Django gives me an error for those characters.

I have to put this line at the top of the Python file to make it work:

# -*- coding: utf-8 -*-

Is there a way to globally allow UTF8 in the whole project?

2
  • You could upgrade to python 3, you'll need to anyway for django 2.0 Commented Dec 9, 2016 at 11:02
  • Check this out https://code.djangoproject.com/wiki/GlobalState Commented Dec 9, 2016 at 11:05

1 Answer 1

2

In Python 2, source files are interpreted as ASCII by default, unless a coding declaration like the one in your question appears at the top of the file.

In Python 3, source files are interpreted as UTF-8 by default (again, unless a coding declaration specifies some other encoding).

Information on the history of this change can be found in PEP 3120: Using UTF-8 as the default source encoding.

There's no other way to force Python to interpret a source file as an encoding other than the default.

So, you have two choices:

  1. Add the coding declaration to every source file which includes non-ASCII characters.

  2. Switch to Python 3.

Option 2 is by far the better choice, and will save you a lot of headache in the long run.

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

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.