Last modified: Sep 10, 2026
How to Import Bool in Python
Understanding the bool Function in Python
In Python, bool is a built-in function used to convert a value into a Boolean value. It returns either True or False. Unlike other libraries, you do not need to import bool because it is part of Python’s core language.
# Using bool without importing it
value = 1
result = bool(value)
print(result)
True
This makes bool readily available in any Python script. However, some developers mistakenly try to import it using the import statement, which leads to errors.
Why You Should Not Import bool
One of the most common mistakes is trying to import bool like this:
from builtins import bool
ImportError: cannot import name 'bool' from 'builtins'
This will result in an ImportError. The reason is that bool is not a module-level object that needs importing. It is always accessible directly in Python.
When Is bool Useful?
The bool function becomes helpful when you want to explicitly check whether a value is considered True or False in a Boolean context. For example:
# Checking truthiness of different values
print(bool(0)) # 0 is falsy
print(bool(1)) # Non-zero integers are truthy
print(bool("")) # Empty strings are falsy
print(bool("Hello")) # Non-empty strings are truthy
print(bool([])) # Empty lists are falsy
print(bool([1, 2])) # Non-empty lists are truthy
False
True
False
True
False
True
These examples show how Python evaluates values as True or False by default. You can also use bool in conditional statements to make logic more explicit.
Using bool with Custom Objects
You can define how your custom classes behave in Boolean contexts by implementing the __bool__ method:
class MyClass:
def __init__(self, value):
self.value = value
def __bool__(self):
return self.value > 0
obj1 = MyClass(5)
obj2 = MyClass(-3)
print(bool(obj1)) # True because value is positive
print(bool(obj2)) # False because value is negative
True
False
This allows you to control how instances of your class are interpreted in Boolean conditions.
Common Mistakes and Fixes
Some developers might confuse bool with Boolean data types in other languages. In Python, there’s no need for an import statement. Just use bool() directly.
Another mistake is assuming that bool can be imported from modules like typing or builtins. Here’s what happens:
import typing
typing.bool(1)
AttributeError: module 'typing' has no attribute 'bool'
To avoid such errors, remember that bool is always available without any import.
Conclusion
In summary, you do not need to import bool in Python. It is a built-in function that converts values into Boolean types. Trying to import it will cause errors. Understanding how bool works helps you write clearer and more reliable code. Whether working with numbers, strings, or custom objects, bool plays a key role in evaluating truthiness.
If you're looking to deepen your knowledge of Python’s Boolean system, check out our guides on Python Booleans: True, False, Logic Guide and Python Boolean Combinations Guide. These resources offer further insights into Boolean operations and logic in Python.