Last modified: Mar 06, 2025 By Alexander Williams

Fix Python NameError: Name 'UUID' Not Defined

Encountering a NameError in Python can be frustrating, especially when the error message says Name 'UUID' is not defined. This error typically occurs when you try to use the UUID module without importing it first. In this article, we'll explore the causes of this error and provide step-by-step solutions to fix it.

What Causes the NameError: Name 'UUID' Not Defined?

The NameError: Name 'UUID' is not defined occurs when Python cannot find the UUID module in your code. This usually happens because you forgot to import the uuid module before using it. The uuid module is part of Python's standard library, but you still need to import it explicitly.

Here's an example of code that would trigger this error:


# This will raise a NameError
my_uuid = UUID('12345678-1234-5678-1234-567812345678')

In this case, Python doesn't know what UUID is because it hasn't been imported. To fix this, you need to import the uuid module.

How to Fix the NameError: Name 'UUID' Not Defined

To resolve this error, you need to import the uuid module before using it. Here's how you can do it:


import uuid

# Now you can use the UUID class
my_uuid = uuid.UUID('12345678-1234-5678-1234-567812345678')
print(my_uuid)

In this corrected code, we first import the uuid module. Then, we create a UUID object using uuid.UUID. This will work without any errors.

Common Mistakes to Avoid

One common mistake is forgetting to import the uuid module. Another mistake is importing the module but using the wrong case. Python is case-sensitive, so UUID and uuid are considered different names.

For example, the following code will still raise a NameError:


import uuid

# This will raise a NameError because 'UUID' is not the same as 'uuid'
my_uuid = UUID('12345678-1234-5678-1234-567812345678')

To avoid this, always use the correct case when referencing the UUID class.

Example with Output

Let's look at a complete example that demonstrates how to use the uuid module correctly:


import uuid

# Generate a random UUID
random_uuid = uuid.uuid4()
print("Random UUID:", random_uuid)

# Create a UUID from a string
string_uuid = uuid.UUID('12345678-1234-5678-1234-567812345678')
print("UUID from string:", string_uuid)

When you run this code, you should see output similar to the following:


Random UUID: 123e4567-e89b-12d3-a456-426614174000
UUID from string: 12345678-1234-5678-1234-567812345678

This output shows that the code is working correctly, and the UUID objects are being created as expected.

Conclusion

The NameError: Name 'UUID' is not defined is a common error that occurs when you forget to import the uuid module or use the wrong case. By ensuring that you import the module correctly and use the right case, you can easily avoid this error.

If you encounter similar errors with other modules, such as Decimal, ElementTree, or webdriver, the solution is often the same: make sure you import the module before using it.

By following the steps outlined in this article, you should be able to fix the NameError: Name 'UUID' is not defined and continue working on your Python projects without any issues.