[Fixed] nameerror: name Unicode is not defined

About the error

The ‘nameerror: name Unicode is not defined’ means that the Python Interpreter couldn’t understand the keyword unicode in the code. This happens for a couple of reasons, which will be discussed later.

Reasons of error

In case you have got the ‘nameerror: name Unicode is not defined’ error, it could be due to the reasons given below:

  • You haven’t imported the Unicode module correctly.
  • No Unicode type exists in Python 3. Python 3 has a str data type instead of Unicode.

Methods of resolving the error

You can check the methods given below to solve this issue.

Change unicode to str

The easiest way to resolve this error is to carry out this replacement.

result = str('car')

Thus, the output will be in string format.


Use bytes() function

In case you have binary data in updated Python versions, you should use the bytes() function.

Ans= 'pythonpool'
Res = bytes(Ans, 'utf-8')

Therefore, you’ll get the output in binary form as:


Set Unicode as str

In this way, you need to equate the Unicode variable to the str class.

import sys

if sys.version_info[0] >= 3:
    unicode = str


From Python 3 onwards, the interpreter accepts the str class and not Unicode.

Use encode() and decode() functions

If you want your data in bytes form, use the str.encode() function, while if you want to decode this back to str form, you can work with the decode() function.

Str1 = 'python'
my_binary_data = Str1.encode('utf-8')
Str2 = my_binary_data.decode('utf-8')

So you’ll get the output as:

b'python' 'python'

nameerror name ‘unicode’ is not defined sublist3r

This issue arises because Sublist3r was designed for Python2. Thus, the Unicode should change to str type. In order to resolve the error, wherever you have written unicode in your code, replace it with str. Otherwise, check for a version of Sublist3r that works on Python3 only.

An alternative way to solve the nameerror name ‘unicode’ is not defined is to create a virtual environment for Python3. Install Sublist3r in this virtual environment, and you can continue using Unicode.

nameerror name ‘unicode’ is not defined coco

A similar problem exists for coco as well. You should consider replacing occurrences of Unicode with str or changing the Python 2 code to that accepted by Python 3. In this case, see the specific syntax and functionalities involved. Unicode will give an error with all previous versions of Python.

# Before (Python 2)
s = unicode("Hello, world!")

# After (Python 3)
s = str("Hello, world!")

# Before (Python 2)
print unicode("This is a string")

# After (Python 3)
print("This is a string")

comtypes nameerror name ‘unicode’ is not defined

This code needs a replacement from unicode(text) to str(text). Also, you can try altering  string literals like u'text' with 'text'. This will remove the nameerror: name Unicode is not defined error while operating on comtypes. You should also check the version of Python using:  python --version or python3 --version.

Other tips while working with Unicode strings

You can check these tips if you are working on Unicode strings.

  • Avoid mixed encodings and use separate functions if it is necessary to have mixed encodings.
  • Check the encoding type once again to prevent errors. This refers to Mojibake.
  • Check for byte order mark (BOM). If it is incorrectly specified, the decoder will misinterpret your text. This comes under bombed encoding.
  • You can work with functions like str.encode(), str.decode(), and unicodedata.normalize() in Python 3. You can normalize your text with the third function.


Why did the unicode type gets renamed to str in Python 3?

This has been altered for the ease of users. Python 3 has all strings as str.


This article covers reasons for “nameerror: name Unicode is not defined.” It covers the ways through which you can get rid of this name error. The best is to use str in place of Unicode, as Python 3 has str strings.

Notify of
Inline Feedbacks
View all comments