185

I am trying to use a Python package called bidi. In a module in this package (algorithm.py) there are some lines that give me error, although it is part of the package.

Here are the lines:

# utf-8 ? we need unicode
if isinstance(unicode_or_str, unicode):
    text = unicode_or_str
    decoded = False
else:
    text = unicode_or_str.decode(encoding)
    decoded = True

and here is the error message:

Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    bidi_text = get_display(reshaped_text)
  File "C:\Python33\lib\site-packages\python_bidi-0.3.4-py3.3.egg\bidi\algorithm.py",   line 602, in get_display
    if isinstance(unicode_or_str, unicode):
NameError: global name 'unicode' is not defined

How should I re-write this part of the code so it works in Python3? Also if anyone have used bidi package with Python 3 please let me know if they have found similar problems or not. I appreciate your help.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
TJ1
  • 7,578
  • 19
  • 76
  • 119

7 Answers7

285

Python 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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • So should I write: if isinstance(unicode_or_str, str)? How about 'unicode_or_str'? – TJ1 Nov 09 '13 at 14:56
  • 1
    The variable name doesn't much matter here; `if isinstance(unicode_or_str, str)` should just work. Renaming the variable name is optional. – Martijn Pieters Nov 09 '13 at 14:57
50

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)
Goblinhack
  • 2,859
  • 1
  • 26
  • 26
  • 2
    This is the right idea, nice answer. Just to add a detail, if you are using the `six` library to manage Python 2/3 compatibility, you can make this: `if six.PY3: unicode = str` instead of `sys.version_info` stuff. This is also very helpful for preventing linter errors related to unicode being undefined in Python 3, without needing special linter rule exemptions. – ely Sep 06 '19 at 18:30
25

You can use the six library to support both Python 2 and 3:

import six
if isinstance(value, six.string_types):
    handle_string(value)
atm
  • 1,684
  • 1
  • 22
  • 24
4

One can replace unicode with u''.__class__ to handle the missing unicode class in Python 3. For both Python 2 and 3, you can use the construct

isinstance(unicode_or_str, u''.__class__)

or

type(unicode_or_str) == type(u'')

Depending on your further processing, consider the different outcome:

Python 3

>>> isinstance(u'text', u''.__class__)
True
>>> isinstance('text', u''.__class__)
True

Python 2

>>> isinstance(u'text', u''.__class__)
True
>>> isinstance('text', u''.__class__)
False
2

Hope you are using Python 3 , Str are unicode by default, so please Replace Unicode function with String Str function.

if isinstance(unicode_or_str, str):    ##Replaces with str
    text = unicode_or_str
    decoded = False
M.J
  • 152
  • 13
  • 2
    that doesn't preserve BC like the answer from @atm Please consider retracting or updating your answer. There is no reason to leave python2 users behind or have breaking python3 – MrMesees Sep 04 '18 at 22:47
1

If a 3rd-party library uses unicode and you can't change their source code, the following monkey patch will work to use str instead of unicode in the module:

import <module>
<module>.unicode = str
0

you can use this in python2 or python3

type(value).__name__ == 'unicode':
lxgxx
  • 11
  • 2