1

Using pythonwin (i have version 3.6.8 pywin32 build224.0 from 2018) write:

print("Hello world, I make £10000 per month")

or what I originally tested

print("Hej världen")

which is Swedish (my language) for "hello world" (the £10000 is an example and not my salary)

Save as a file. Close and reopen it and run it. There is a fault message:

Failed to run script - syntax error (unicode error) 'utf-8' codec can't decode byte 0xa3 in position 22: invalid start byte (helloworld£10000.py)

It seems that the editor uses Latin-1 and the python interpreter uses utf-8.

Using notepad to save it as UTF-8 and reopen in pythonwin allows running but the source becomes: Hello world, I make £10000 per month

This has to be a frequent problem but I can't find a good way of handling it for pythonwin. Neither in help or on stackoverflow. I saw a discussion on the issue on unicode in pythonwin is from 2003 and I don't understand why this is a problem now.

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Python use UTF-8 as default (you can use magic codes at beginning of file to change interpretation. Then the second problem: you use print, and print in windows will use system code page (Windows is the only large OS which it is not using UTF-8 as default). So you should search this forum on how to print UTF-8 (so setting terminal or using other terminal so that they are UTF-8) – Giacomo Catenazzi Jun 10 '21 at 08:41

2 Answers2

1

Pythonwin in older versions of pywin32 saved in the localized ANSI encoding unless a #coding: comment specifies something else. Pythonwin reads that comment and saves in the encoding declared, but note I encountered at least one older version that was buggy about that as well. For UTF-8 use:

#coding: utf8

Ideally, use the latest version (301 at this writing), which defaults to UTF-8.

If you install/upgrade via pip as in pip install -U pywin32 make sure to open an administrator prompt and run Scripts\pywin32_postinstall.py -install if you want things like import win32com to work.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
0

I found a solution. Close the file in pythonwin. In windows 10 open in notepad and save as "UTF-8 with BOM" (I have that). In windows 7 open in notepad and save as "UTF-8".

This will save as UTF-8 with Byte Order Mark. Open in Pythonwin, should work now. . You can even use non-latin like Russian, e.g. print("Привет") now, which the editor does not accept by default (latin-1).

Pythonwin does not save in UTF-8 even with non-latin letters present, unless there is a UTF8-BOM present, and there's no option in my version to save as UTF-8.

  • Actually sometimes this does not work. Sometimes pythonwin says Syntax error in line 1, even if line 1 is perfect or empty. I found that you can write in line 1 # coding: utf-8 or # -*- coding: UTF-8 -*- See also https://stackoverflow.com/questions/728891/correct-way-to-define-python-source-code-encoding Which is 12 years old. Why did pythonwin (3 years ago) still have this bug so you can't write Hello World except in English. – Bengt-Inge Larsson Jun 12 '21 at 21:19