close

[Solved] NameError: global name ‘unicode’ is not defined – in Python 3

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error NameError: global name ‘unicode’ is not defined – in Python 3 in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How NameError: global name ‘unicode’ is not defined – in Python 3 Error Occurs?

Today I get the following error NameError: global name ‘unicode’ is not defined – in Python 3 in python.

How To Solve NameError: global name ‘unicode’ is not defined – in Python 3 Error ?

  1. How To Solve NameError: global name 'unicode' is not defined – in Python 3 Error ?

    To Solve NameError: global name 'unicode' is not defined – in Python 3 Error Python 3 renamed the unicode type to str, the old str type has been replaced by bytes.

  2. NameError: global name 'unicode' is not defined – in Python 3

    To Solve NameError: global name 'unicode' is not defined – in Python 3 Error Python 3 renamed the unicode type to str, the old str type has been replaced by bytes.

Solution 1

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.

Solution 2

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)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read