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

avatar
Borislav Hadzhiev

2 min

banner

# 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.

# Uninstall the enum34 module

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

shell
pip uninstall -y enum34 # ๐Ÿ‘‡๏ธ for python 3 pip3 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

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.

main.py
from enum import IntEnum class Numbers(IntEnum): ONE = 1 TWO = 2 THREE = 3 print(Numbers.THREE) # ๐Ÿ‘‰๏ธ Numbers.THREE print(Numbers.THREE + 10) # ๐Ÿ‘‰๏ธ 13

# Make sure you don't have a local file called enum.py

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__)

If you have a local file named enum.py, the output will be similar to the following.

main.py
# โ›”๏ธ 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.

main.py
# โœ… result if pulling in the correct module # /usr/lib/python3.10/enum.py

# Unset the PYTHONPATH environment variable

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

Open your terminal and run the following command.

shell
unset PYTHONPATH

The PYTHONPATH environment variable determines where the Python interpreter looks for libraries.

Unsetting the variable sometimes fixes glitches on machines with multiple Python versions.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2023 Borislav Hadzhiev