Search code examples
pythonwindowsencodingpycharmbyte-order-mark

Pycharm adding mandatory BOM when creating files from terminal


After I updated Pycharm to version 2021.2, whenever I create a new .py file on Pycharm terminal with echo, I can't run it because of the following error:

SyntaxError: Non-UTF-8 code starting with '\xff' in file [file path...] on line 1, but no encoding declared;

After looking it up I'm convinced that pycharm is adding a mandatory BOM to the created file.

Things I tried:

Going to File -> File Properties -> Remove BOM (It's unable to remove)

Going to File -> File Properties -> File Encoding and changing it to UTF-8 (it gives me the following pop-up): enter image description here

Can't convert it either.

Going to Help -> Edit Custom VM Options and adding -Dconsole.encoding=UTF-8 to it

Creating a new python file by clicking works fine.
Creating a new file with echo on cmd terminal works fine too.

What is causing this? How do I solve it? I didn't have this problem before updating Pycharm.


Solution

  • As Giacomo Catenazzi pointed out, changing the terminal in Settings -> tools -> terminal to either cmd.exe or git-cmd.exe (from powershell.exe) worked perfectly.

    I can now create files using echo on the terminal again without it being set to UTF-16 and without BOM.