1

I have a working script at hand, while it runs on the Spyder IDE and python shell, when I just run it by double clicking, it closes right away. To understand the problem, I ran it through the cmd prompt and encountered the following:

Traceback (most recent call last):
File "C:\Users\Cheese\Desktop\demografik-proje\demo-form-v-0-1-3.py", line 314, in <module>
mainMenu(q_list, xtr_q_list)
File "C:\Users\Cheese\Desktop\demografik-proje\demo-form-v-0-1-3.py", line 152, in mainMenu
patient_admin = input("Testi uygulayan ki\u015fi: ") #the person who administrated the test
File "C:\Program Files\Python35\lib\encodings\cp850.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character '\u015f' in position 18: character maps to <undefined>

This question has been asked many times before, but why I'm asking is, this script works fine in some computers, just by double-clicking, yet doesn't work on mine, for instance. From what I gathered, could it be related to the fact that my computer is in English, yet the computers that were able to run were in Turkish?

Also since program has many Turkish strings, I'd rather not fiddle with every individual string and rather put a thing on the top or something. I'm even up for setting up a batch file to run the script in UTF8. Or if I could freeze it in such a way that it recognizes UTF8(this would be preferred)? Also, I just checked, and the program works fine if all the turkish chars are removed. As expected.

If it's any help, Spyder still runs Python 3.5.1, I have 3.5.2 installed and when I just type "python" on command prompt, Python 3.5.2 runs just fine.

Following is the code, if it's any assistance:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Block o' Code
"""
    patient_admin = input("Testi uygulayan kişi: ") #the person who administrated the test

    #gets input for all the data program needs    
    print("=======================================")
"""
More block o' code
"""
  • The problem is the codeset which `cmd.exe` is using. – cdarke Jul 24 '16 at 07:34
  • Your code is (probably) fine (I'm not going to read through all that stuff, you should illustrate your problem with a [mcve]). But you need to fix the terminal that the code runs in so that it uses a UTF-8 code page. I'm not a Windows user, but there are various SO answers that show how to do that via editing the registry. In an interactive command prompt you can just run `chcp 65001` before executing your Python scripts; obviously that's not an option when starting a script via-double-clicking. – PM 2Ring Jul 24 '16 at 07:36
  • The answers to [How to make Unicode charset in cmd.exe by default?](http://stackoverflow.com/questions/14109024/how-to-make-unicode-charset-in-cmd-exe-by-default) appear to have a few solutions. – PM 2Ring Jul 24 '16 at 07:41
  • I've made the code a bit more minimalistic, just including the part that didn't work. Also, I tried the solutions that were given there, none helped. – Bilal Bahadır Akbulut Jul 24 '16 at 08:24
  • Possible duplicate of [Python, Unicode, and the Windows console](http://stackoverflow.com/questions/5419/python-unicode-and-the-windows-console) – roeland Jul 24 '16 at 22:01

2 Answers2

0

Okay, found the solution, wonder how I missed this one (considering I tried to figure this one out for 2-3 hours).

To run the program with a non ASCII strings, you can change the code page for the command prompt. To do that: Open Windows Control Panel Select Region and Language Click on the Administrative tab Under Language for non-Unicode programs, click on Change System Locale Choose the locale (Turkish for my case) Click OK

It's going to restart your computer, after that, your program should work fine.

Source: http://knowledgebase.progress.com/articles/Article/4677

  • Genius friend : did you want run script on other machines ? – dsgdfg Jul 24 '16 at 08:28
  • Well, I'd rather have a widespread solution, but since this program will be used in limited amount of computers in our clinic, we should be fine. It's going to be publicly distributed as well, but people can switch the language setting if they want to use the program. – Bilal Bahadır Akbulut Jul 24 '16 at 08:31
  • Move your app to web side for all devices. No encoding shit, no permission, no additional files etc. – dsgdfg Jul 24 '16 at 08:33
  • One of the reasons we are using this is to make it offline. Also, that requires hosting, and fiddling with other things which I want no part of. – Bilal Bahadır Akbulut Jul 24 '16 at 08:36
0

input with a string argument does a print, and, notoriously, PrintFails.

The Windows command prompt is hopelessly broken at Unicode. You can change the code page it uses to one that includes the characters you want to print, for example using the command chcp 1254 for the legacy Turkish encoding code page 1254, before running the Python script. By setting the ‘Language for non-Unicode programs’ you are setting this code page as the default for all command prompts.

However this will still fail if you need to use characters that don't exist in 1254. In theory as @PM2Ring suggests you could use code page 65001 (which is nearly UTF-8), but in practice the long-standing bugs in Windows's implementation of this code page usually make it unusable.

You could also try installing the win-unicode-console module, which attempts to work around the problems of the Windows command prompt.

bobince
  • 528,062
  • 107
  • 651
  • 834
  • Hmm, I see. Well, chcp or changing code page otherwise works, but what I'm curious about is if there's a way of doing this without relying on the end user to change something on his/her computer. I can change the settings on our computers here in the clinic, but if I want to distribute it, it may be annoying for some people(even though most of the computers probably have their default as OEM857) – Bilal Bahadır Akbulut Jul 24 '16 at 09:52
  • The Windows console (conhost) is just broken and MS have shown no interest in fixing it over many major updates. You could perhaps ship `win-unicode-console` with your app (or directly call the Win32-specific `WriteUnicodeW` API via ctypes when you detect your app is running from a command prompt, which is essentially what win-unicode-console is doing). Although you still have the issue of the default console bitmap font being useless. – bobince Jul 24 '16 at 10:05