The error message is pointing to a line in boilerpipe/extract/__init__.py, which makes a call to the unicode built-in function.
I assume the link below is the source code for the package you are using. If so, it appears to be written for Python 2.7, which you can see if you look near the end of this file:
https://github.com/misja/python-boilerpipe/blob/master/setup.py
You have several options as far as I can see:
- Find a Python 3 port of this package. There are at least a few out there (here's one and here's another).
- Port the package to Python 3 yourself (if that is the only error, you could simply change that line to use
str, but later changes could cause problems with other parts of the package). This official tool should be of assistance; this official guide should, as well. - Port you project to Python 2.7 and continue using the same package.
I hope this helps!
Answer from Collin R on Stack OverflowPython 3 renamed the unicode type to str, the old str type has been replaced by bytes.
if isinstance(unicode_or_str, str):
text = unicode_or_str
decoded = False
else:
text = unicode_or_str.decode(encoding)
decoded = True
You may want to read the Python 3 porting HOWTO for more such details. There is also Lennart Regebro's Porting to Python 3: An in-depth guide, free online.
Last but not least, you could just try to use the 2to3 tool to see how that translates the code for you.
If you need to have the script keep working on python2 and 3 as I did, this might help someone
import sys
if sys.version_info[0] >= 3:
unicode = str
and can then just do for example
foo = unicode.lower(foo)
in python 3 strings are unicode by default.
Remove the unicode function, replace by str.
https://docs.python.org/3.0/whatsnew/3.0.html#text-vs-data-instead-of-unicode-vs-8-bit
There's also a little recipe to make the code python 2 & 3 compatible:
try:
unicode # check if unicode is defined
except NameError: # not found: python 3: replace by str
unicode = str
Like Bakuriu said in his comment, never use a bare except:
Prefer:
except Exception as e:
print("Problem "+repr(e))
# the line below requires some HTML normalization or resulting
# html could be incorrect
import re
ne = re.sub("[^\w]"," ",str(e))
self.browser.append("<font color=red>"+ne+"</font>")
Now you have the real/next exception displayed.
Hope you are using Python 3 , so please replace
Unicode function with String Str function.
def updateUi(self):
try:
text = str(self.lineedit.text()) ##replaced here
unicode is a python 2 method. If you are not sure which version will run this code, you can simply add this at the beginning of your code so it will replace the old unicode with new str:
import sys
if sys.version_info[0] >= 3:
unicode = str
unicode is python 2.x method. If you are running Python 3.x, then all strings are unicode and that call is not needed.
https://docs.python.org/3/howto/unicode.html
As has already been pointed out in the comments, there is already advice on porting from 2 to 3.
Having recently had to port some of my own code from 2 to 3 and maintain compatibility for each for now, I wholeheartedly recommend using python-future, which provides a great tool to help update your code (futurize) as well as clear guidance for how to write cross-compatible code.
In your specific case, I would simply convert all calls to unicode to use str and then import str from builtins. Any IDE worth its salt these days will do that global search and replace in one operation.
Of course, that's the sort of thing futurize should catch too, if you just want to use automatic conversion (and to look for other potential issues in your code).
You can test whether there is such a function as unicode() in the version of Python that you're running. If not, you can create a unicode() alias for the str() function, which does in Python 3 what unicode() did in Python 2, as all strings are unicode in Python 3.
# Python 3 compatibility hack
try:
unicode('')
except NameError:
unicode = str
Note that a more complete port is probably a better idea; see the porting guide for details.