Reading timeยท2 min
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.
enum34
module as it might be shadowing the official enum
module.enum34
moduleOpen 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 enum
module.
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
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.
import enum print(enum.__file__)
If you have a local file named enum.py
, the output will be similar to the
following.
# โ๏ธ 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
PYTHONPATH
environment variableIf none of the suggestions helped, try to unset the PYTHONPATH
environment
variable.
Open your terminal and run the following command.
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.
You can learn more about the related topics by checking out the following tutorials: