Fix NameError: name ‘unicode’ Is Not Defined in Python 3

Quick answer: Python 3 uses str for text and bytes for binary data, so the Python 2 unicode name is not defined. Replace text-oriented unicode calls with str and decode bytes explicitly at the input boundary.

Python unicode NameError infographic comparing Python 2 unicode, Python 3 str, bytes decode, and compatibility migration
Python 3 text is str and binary data is bytes; migrate the boundary explicitly instead of treating unicode as a missing package.

The error NameError: name 'unicode' is not defined happens when Python 3 code tries to use the old Python 2 unicode type. In Python 3, text strings are str. There is no separate built-in unicode type.

The usual fix is to replace unicode with str. If you are working with raw bytes, decode those bytes into text first.

Why unicode is not defined in Python 3

In Python 2, str usually meant byte strings and unicode meant text strings. Python 3 simplified this model: str is text, and bytes is binary data.

The official Python Unicode HOWTO explains that since Python 3.0, the str type contains Unicode characters. The official str documentation covers Python’s text sequence type.

Fix 1: Replace unicode() with str()

If your code calls unicode(), replace it with str() in Python 3.

# Python 2 style
# value = unicode(123)

# Python 3
value = str(123)
print(value)
print(type(value))

Output:

123
<class 'str'>

Python Pool infographic comparing Python 2 unicode, Python 3 str, bytes, and source text
Python 2 name: Python 2 unicode, Python 3 str, bytes, and source text.

Fix 2: Replace isinstance(value, unicode)

Old Python 2 code often checks whether a value is text like this:

# Python 2 style
# isinstance(value, unicode)

In Python 3, use str:

value = "Python Pool"

if isinstance(value, str):
    print("text")

The official isinstance() documentation covers type checks.

Fix 3: Decode bytes into str

If the value is binary data, do not blindly wrap it with str(). Decode it with the correct encoding.

data = b"pythonpool"
text = data.decode("utf-8")

print(text)
print(type(text))

Output:

pythonpool
<class 'str'>

To convert text back to bytes, encode it:

text = "pythonpool"
data = text.encode("utf-8")

print(data)

Output:

b'pythonpool'

The official bytes documentation explains Python’s binary sequence type.

Fix 4: Add a compatibility alias only for legacy code

If you maintain old code that must support both Python 2 and Python 3, you may see this compatibility pattern:

try:
    unicode
except NameError:
    unicode = str

This makes unicode point to str when the code runs on Python 3. Use this only when you are maintaining legacy code. For new Python 3 code, write str directly.

Python Pool infographic converting Unicode text through encoding, bytes, decoding, and output
Text boundary: Python Pool infographic converting Unicode text through encoding, bytes, decoding, and output.

When the error comes from a package

If the traceback points into an installed package, that package may still contain Python 2-era code. Upgrade the package first:

python -m pip install --upgrade package-name

If the package has not been updated for Python 3, look for a maintained fork or replacement. Running an unmaintained Python 2 tool inside a modern Python 3 project usually causes more errors than it solves.

If your terminal itself is using the wrong interpreter, see python is not recognized as an internal or external command for Windows setup and PATH troubleshooting.

Sublist3r, comtypes, and other old code

Many reports of this error come from older tools that were originally written for Python 2. The direct code replacement is usually:

# Old
# text = unicode(value)

# New
text = str(value)

But if the package has many Python 2 assumptions, fixing only unicode may not be enough. Check whether the project documents Python 3 support before patching files manually.

Python Pool infographic mapping a Python 2 unicode call to Python 3 str and explicit encoding
Migration path: A Python 2 unicode call to Python 3 str and explicit encoding.

Common mistakes

  • Importing a Unicode module: there is no built-in module you need to import to make unicode work in Python 3.
  • Using bytes() when you really need text: bytes is binary data. Use str for text.
  • Calling str() on bytes without decoding: str(b"abc") gives a representation like "b'abc'", not decoded text.
  • Keeping compatibility aliases in new code: use str directly when your project is Python 3 only.
  • Ignoring the traceback: find whether the bad unicode reference is in your code or inside a dependency.

Related string and NameError guides

For more string work, see Python ord(), remove Unicode characters in Python, and remove newline from a list. For another NameError-style package issue, read NameError: name NLTK is not defined.

Conclusion

To fix NameError: name 'unicode' is not defined, replace unicode with str in Python 3. Use bytes.decode() when converting bytes into text, and reserve the unicode = str alias for legacy compatibility code only.

Replace Python 2 Text Names

In Python 3, a text literal is str and a binary literal is bytes. Code that calls unicode(value) usually wants str(value), but the correct fix depends on whether the input is already text or represents encoded bytes.

value = 42
text = str(value)
print(text, type(text))

raw = b"caf\xc3\xa9"
decoded = raw.decode("utf-8")
print(decoded)
Python Pool infographic testing source encoding, non-ASCII text, files, and compatibility behavior
Text checks: Source encoding, non-ASCII text, files, and compatibility behavior.

Decode Bytes Once

Decode bytes with the encoding used by the source. Do not call decode on a str, and do not encode and decode repeatedly in the middle of business logic. Keeping the boundary explicit prevents both NameError and mojibake caused by the wrong codec.

def read_name(raw):
    if isinstance(raw, bytes):
        return raw.decode("utf-8")
    if isinstance(raw, str):
        return raw
    raise TypeError("expected text or bytes")

print(read_name(b"Python"))
print(read_name("Pool"))

Migrate Compatibility Code Deliberately

A short-lived compatibility alias can help a dual-version project, but it should not hide an incomplete migration. Prefer supported tooling and tests that exercise text, bytes, file, and network boundaries. Python 2 support is obsolete, so new code should target Python 3 directly.

try:
    text_type = unicode
except NameError:
    text_type = str

print(text_type("Python"))

Python’s str documentation and bytes documentation define the Python 3 text boundary.

For related text boundaries, compare removing Unicode characters, bytes-to-string decoding, and case conversion after deciding whether a value is text or bytes.

Frequently Asked Questions

Why is unicode not defined in Python 3?

Python 3 uses str for text and bytes for binary data, so the Python 2 unicode name is not a built-in name.

What replaces unicode in Python 3?

Use str for text values and decode bytes with an explicit encoding such as bytes_value.decode(‘utf-8’).

How do I write compatibility code for Python 2 and 3?

Prefer a supported migration tool or a narrowly scoped compatibility alias only when the project truly must run on both versions.

Should I decode every Python string?

No. Decode bytes at the input boundary; a value that is already str should not be decoded again.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted