Printing utf8 strings in Sublime Text's console with Windows

前端 未结 3 1074
自闭症患者
自闭症患者 2021-01-13 07:10

When running this code with python myscript.py from Windows console cmd.exe (i.e. outside of Sublime Text), it works:

# co         


        
3条回答
  •  醉酒成梦
    2021-01-13 07:29

    I have found a possible fix: add the encoding parameter in the Python.sublime-build file:

    {
    "cmd": ["python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
    "encoding": "cp1252",
    ...
    

    Note: "encoding": "latin1" seems to work as well, but - I don't know why - "encoding": "utf8" does not work, even if the .py file is UTF8, even if Python 3 uses UTF8, etc. Mystery!


    Edit: This works now:

    {
      "cmd": ["python", "-u", "$file"],
      "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
      "selector": "source.python",
      "encoding": "utf8",
      "env": {"PYTHONIOENCODING": "utf-8", "LANG": "en_US.UTF-8"},
    }
    

    Linked topic:

    • Setting the correct encoding when piping stdout in Python and this answer in particular

    • How to change the preferred encoding in Sublime Text 3 for MacOS for the env trick.

提交回复
热议问题