2
votes

TL, DR: A python file encounters the UnicodeDecodeError when ran in Spyder, but works when I ran it in command line.


I downloaded a Python module that wraps a C implementation of a suffix tree. After built, it was ran in Spyder but I got the following error:

runfile('F:/src/suffix_tree-2.1/build/lib.win32-2.7/suffix_tree.py', wdir='F:/src/suffix_tree-2.1/build/lib.win32-2.7') Traceback (most recent call last):

File "G:\IDE\python\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3052, in run_code self.showtraceback()

File "G:\IDE\python\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 1851, in showtraceback value, tb, tb_offset=tb_offset)

File "G:\IDE\python\Anaconda\lib\site-packages\IPython\core\ultratb.py", line 1240, in structured_traceback self, etype, value, tb, tb_offset, number_of_lines_of_context)

File "G:\IDE\python\Anaconda\lib\site-packages\IPython\core\ultratb.py", line 1157, in structured_traceback self, etype, value, elist, tb_offset, number_of_lines_of_context

File "G:\IDE\python\Anaconda\lib\site-packages\IPython\core\ultratb.py", line 511, in structured_traceback lines = ''.join(self._format_exception_only(etype, value))

File "G:\IDE\python\Anaconda\lib\site-packages\IPython\core\ultratb.py", line 623, in _format_exception_only Colors.Normal, s))

UnicodeDecodeError: 'ascii' codec can't decode byte 0xb2 in position 20: ordinal not in range(128)

This error appears right after I import _suffix_tree.pyd, even before any other operations.

However, if I run the file in command line (cmd), it works successfully without any error.

PS: I use Windows, and my user name are ASCII characters.

1
Don't know if this help, probably not but have you tried to put # -- coding: utf-8 -- on the very top of the file (before imports)?Milano
It doesn't unfortunately. :-(CodeRush
I had a similar problem (I have an accent in my username). I used Anaconda3 to solve it.clemtoy
Thanks @clemtoy. I thought it was an issue in Spyder, since my file works in PyCharm. I will try Anaconda3 too.CodeRush

1 Answers

1
votes

You are possibly trying to open a file encoded in a different format of your system, you should check it and set Spyder using the commands below.

import sys  
reload(sys)  
sys.setdefaultencoding('utf8')

If this does not work, check the version of the Python kernel of the program. You can get this message trying to run an Python 3 code in an Spyder 2.