0

I think it's sublime related rather than python, running this snippet:

x = "Buenos días"
print(x)

will print in terminal/command prompt but not in sublimes build results. Did already a little bit of research but couldn't find a working solution. Removing the í acute accent to a i works as expected.

user1767754
  • 23,311
  • 18
  • 141
  • 164
  • Do you see an encoding error? Are you using the built in Python build system or did you make your own? – OdatNurd Jun 08 '18 at 21:26
  • I don't see any encoding errors and it's an install I did and I've added the build system as well based on https://stackoverflow.com/questions/23730866/set-up-python-3-build-system-with-sublime-text-3 – user1767754 Jun 08 '18 at 21:27
  • Try adding this to your build system if its not already there: `"env": {"PYTHONIOENCODING": "utf-8"},` – OdatNurd Jun 08 '18 at 21:29
  • OMG, that actually worked, please provide that as an answer. – user1767754 Jun 08 '18 at 21:31

1 Answers1

1

Generally speaking problems like this are caused by some interplay between how Python determines behind the scenes the encoding that it should use when it's generating output and how Sublime is executing the Python interpreter.

In particular where it may correctly determine the correct encoding when run from a terminal, the Python interpreter may get confused and pick the wrong one when Sublime invokes it.

The PYTHONIOENCODING environment variable can be used to tell the interpreter to use a specific encoding in favor of whatever it might have otherwise automatically selected.

The sublime-build file lets you specify custom environment variables to apply during a build using the env key, so you can do something like the following:

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

    "env": {"PYTHONIOENCODING": "utf-8"},

    "variants":
    [
        {
            "name": "Syntax Check",
            "shell_cmd": "python -m py_compile \"${file}\"",
        }
    ]
}
OdatNurd
  • 21,371
  • 3
  • 50
  • 68