Is it possible to avoid having to put this in every page?
# -*- coding: utf-8 -*-
I’d really like Python to default to this.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In Python 3, the default encoding is UTF-8, so you won’t need to set it explicitly anymore. There isn’t a way to ‘globally’ set the default source encoding, though, and history has shown that such global options are generally a bad idea. (For instance, the -U and -Q options to Python, and sys.setdefaultencoding() back when we had it.) You don’t (directly) control all the source that gets imported in your program, because it includes the standard library and any third-party modules you use directly or indirectly.
Also note that this isn’t enabling Unicode, as your question title suggests. What it does is make the source encoding UTF-8, meaning that any non-ASCII characters in unicode literals (e.g.
u'spæm') will be interpreted using that encoding. It won’t make non-unicode literals ('spam'and"spam") suddenly unicode, nor will it do anything for non-literals anywhere in your code.