Search code examples
pythonunicodeencodingpep

Encoding issue in python


So, I read PLENTY of questions and every single one of them forgot to put # coding: <encoding> string in start, or something like that.

My problem is this, chronologically.

1) I forgot to put encoding too, while working with cyrillic literals

2) I put it: didn't work

3) I put from __future__ import unicode_literals Still doesn't works.

4) I commented out string, which caused it. Didn't work. Shocked.

5) I rolled back (manually) to previous version of mine, deleted all changes completely. Don't work.

So, this is my traceback, and in 1st line I only have import tweepy which clearly doesn't have any unicode literals (deleted shebang in process of finding the cause of issue)

    Traceback (most recent call last):
  File "E:\Coding\PyCharm 3.1\helpers\pydev\pydevd.py", line 1534, in <module>
    debugger.run(setup['file'], None, None)
  File "E:\Coding\PyCharm 3.1\helpers\pydev\pydevd.py", line 1145, in run
    pydev_imports.execfile(file, globals, locals) #execute the script
  File "C:/Users/tibur_000/PycharmProjects/Twelcome/TWelcome.py", line 1
SyntaxError: Non-ASCII character '\xfe' in file
C:/Users/tibur_000/PycharmProjects/Twelcome/TWelcome.py on line 1,
but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Solution

  • It looks like your file starts with the BOM character; you probably saved the file in a UTF encoding.

    If you are not actually using non-ASCII text in your source, you may want to switch back to using the ASCII encoding for your file. Otherwise, you'll have to specify the UTF codec used in a PEP 263 codec declaration.

    However, if it is using UTF-16 or UTF-32, pick a different codec. Python 2 does not support source code encoded to these encodings.

    In PyCharm, you can alter the encoding used for individual files via the File > File encoding menu option or the status bar. See the PyCharm help information