To solve the "AttributeError: module 'enum' has no attribute 'IntFlag'",
enum34 module by running
pip uninstall -y enum34 in your
If the error persists, make sure you don't have an
enum.py file in your
enum34module as it might be shadowing the official
Open your terminal in your project's root directory and run the following command.
pip uninstall -y enum34 # 👇️ for python 3 pip3 uninstall -y enum34
Now try importing and using the
Enum class from the
from enum import Enum class Sizes(Enum): SMALL = 'sm' MEDIUM = 'md' LARGE = 'lg' print(Sizes.MEDIUM.name) # 👉️ MEDIUM print(Sizes.MEDIUM.value) # 👉️ md
You can also use the IntEnum class.
It is the same as Enum, but its members are integers and can be used anywhere an integer can be used.
from enum import IntEnum class Numbers(IntEnum): ONE = 1 TWO = 2 THREE = 3 print(Numbers.THREE) # 👉️ Numbers.THREE print(Numbers.THREE + 10) # 👉️ 13
If the error persists, make sure you don't have a local file called
that would shadow the
You can access the
__file__ property on the imported module to see whether it
is shadowed by a local file.
import enum print(enum.__file__)
If you have a local file named
enum.py, the output will be similar to the
# ⛔️ result if shadowed by local file # /home/borislav/Desktop/bobbyhadz_python/enum.py
If you don't have a local file that shadows the
enum module, you should see
something similar to the following.
# ✅ result if pulling in the correct module # /usr/lib/python3.10/enum.py
If none of the suggestions helped, try to unset the
Open your terminal and run the following command.
PYTHONPATH environment variable determines where the Python interpreter
looks for libraries.
Unsetting the variable sometimes fixes glitches on machines with multiple Python versions.
You can learn more about the related topics by checking out the following tutorials: