"UnicodeDecodeError" when starting the python shell on the Windows cmd

1

Good day, I have a problem when initializing the python shell in the Windows cmd, less than a day ago it worked perfectly, but suddenly, when I wanted to start the Python shell inside the Windows cmd with the command "python" ", it shows me the following:

C:\Users\nombreusuario>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Failed calling sys.__interactivehook__
Traceback (most recent call last):
  File "C:\Users\nombreusuario\AppData\Local\Programs\Python\Python35\lib\site.py", line 419, in register_readline
    readline.read_history_file(history)
  File "C:\Users\nombreusuario\AppData\Local\Programs\Python\Python35\lib\site-packages\pyreadline\rlmain.py", line 165, in read_history_file
    self.mode._history.read_history_file(filename)
  File "C:\Users\nombreusuario\AppData\Local\Programs\Python\Python35\lib\site-packages\pyreadline\lineeditor\history.py", line 82, in read_history_file
    for line in open(filename, 'r'):
  File "C:\Users\nombreusuario\AppData\Local\Programs\Python\Python35\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1932: character maps to <undefined>
>>>

And as you can see, in the end I can use the Python shell, but it is obvious that there is a bug, do you know how I can solve it?

    
asked by Jesús Fragoso 07.11.2018 в 17:59
source

1 answer

1

I do not think you need to reinstall anything. You have version 3.5 that, although it is not the last one, is by no means obsolete. Version 3.7 has new things like f-strings that are very good, but if reinstalling is a disturbance, try the following before.

The problem appears because Python, in interactive mode (and only in this case) loads the readline module so that the user experience is more friendly, since it allows you to recover commands, edit them more comfortably, autocomplete with TAB, etc.

The problem in your case is that readline keeps a history of commands that you have given in previous sessions, and saves it in a user file. Each time you start a new Python interpreter, that file is read so you can retrieve commands from previous sessions. At some point in your file was introduced a rare character that now can not decode correctly and that's why it breaks.

Simply delete that history file. If I'm not wrong, you should find it in your user folder, with the name .python_history . If you do not know what your user folder is, you can try running the following command:

python -c "import os; print(os.path.expanduser('~'))"
    
answered by 08.11.2018 / 09:42
source