AttributeError: module 'enum' has no attribute 'IntFlag'

avatar

Borislav Hadzhiev

Last updated: Jun 17, 2022

banner

Photo from Unsplash

AttributeError: module 'enum' has no attribute 'IntFlag' #

To solve the "AttributeError: module 'enum' has no attribute 'IntFlag'", uninstall the enum34 module by running pip uninstall -y enum34 in your terminal. If the error persists, make sure you don't have an enum.py file in your project.

The first thing to try is to uninstall the enum34 module as it might be shadowing the official enum module.

Open your terminal in your project's root directory and run the following command.

shell
pip uninstall -y enum34

Now try importing and using the Enum class from the enum module.

main.py
from enum import Enum class Sizes(Enum): SMALL = 'sm' MEDIUM = 'md' LARGE = 'lg' print(Sizes.MEDIUM.name) # 👉️ MEDIUM print(Sizes.MEDIUM.value) # 👉️ md
If the error persists, make sure you don't have a local file called enum.py as that would shadow the enum module.

You can access the __file__ property on the imported module to see whether it is shadowed by a local file.

main.py
import enum print(enum.__file__) # ⛔️ result if shadowed by local file # /home/borislav/Desktop/bobbyhadz_python/enum.py # ✅ result if pulling in correct module # /usr/lib/python3.10/enum.py

If none of the suggestions helped, try to unset the PYTHONPATH environment variable.

Open your terminal and run the following command.

shell
unset PYTHONPATH

This sometimes fixes glitches on machines with multiple Python versions.

Conclusion #

To solve the "AttributeError: module 'enum' has no attribute 'IntFlag'", uninstall the enum34 module by running pip uninstall -y enum34 in your terminal. If the error persists, make sure you don't have an enum.py file in your project.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.