Skip to main content

How to Resolve "AttributeError: module 'enum' has no attribute 'IntFlag'"

When working with Python's enum module, you may encounter the AttributeError: module 'enum' has no attribute 'IntFlag'. This error occurs when your Python environment attempts to find the IntFlag class within the enum module but fails. This typically happens for one of three main reasons: your Python version is too old, a conflicting legacy package is installed, or a local file in your project is overriding the standard library module.

This guide will explain the origins of IntFlag and provide a systematic troubleshooting process to identify and resolve the specific cause of this error in your environment.

Understanding the Error: The Introduction of IntFlag in Python 3.6

The enum module itself was introduced in Python 3.4. However, the specific IntFlag class, which provides integer-based members that can be combined using bitwise operators, was not added to the standard library until Python 3.6. This versioning detail is the root cause of most occurrences of this error.

Cause 1: Python Version is Older than 3.6

If your Python interpreter is version 3.5 or older, the IntFlag class simply does not exist in the built-in enum module.

Example of code causing the error:

# This code will fail if run on Python 3.5 or older.
import enum

class Permissions(enum.IntFlag):
READ = 1
WRITE = 2
EXECUTE = 4

Output on Python 3.5:

Traceback (most recent call last):
File "main", line 2, in <module>
AttributeError: module 'enum' has no attribute 'IntFlag'

Solution:

  1. Upgrade Python (Highly Recommended): The best solution is to upgrade your environment to a modern, supported version of Python (3.8+). This will give you access to IntFlag and many other modern features and security updates.

  2. Use IntEnum as a Workaround: If you cannot upgrade, you can use IntEnum instead. Be aware that IntEnum creates an enumeration where members cannot be combined with bitwise operators like flags can.

    import enum

    class Permissions(enum.IntEnum):
    READ = 1
    WRITE = 2
    EXECUTE = 4

    # Note: Bitwise operations are not supported with IntEnum
    # combined = Permissions.READ | Permissions.WRITE # This would fail

Cause 2: Conflicting enum34 Package is Installed

The enum34 package is a backport of the enum module for older Python versions (pre-3.4). If this package is installed in a modern Python environment (3.6+), it can "shadow" the standard library's built-in enum module. This means import enum will incorrectly load the old enum34 package, which does not contain IntFlag.

Solution: uninstall the legacy enum34 package. This will allow your system to correctly find and use the built-in enum module that comes with your modern Python installation.

pip uninstall -y enum34

After uninstalling, your existing code should run without any changes.

Cause 3: A Local File Named enum.py is Shadowing the Standard Library

If you have a file named enum.py in your project's directory, Python's import system will find and load your local file instead of the standard library's enum module.

Diagnosing the Problem: you can diagnose this by printing the __file__ attribute of the imported module.

import enum

# This will show you which file is being used for the 'enum' module.
print(enum.__file__)
  • Incorrect Output (Shadowing): /path/to/your/project/enum.py
  • Correct Output (Standard Library): /path/to/your/python/installation/lib/python3.10/enum.py

Solution: rename your local file to something that does not conflict with a standard library module (e.g., my_enums.py).

Troubleshooting: Checking the PYTHONPATH Environment Variable

The PYTHONPATH environment variable tells the Python interpreter where to look for modules. If this variable is misconfigured to point to an old Python installation or a directory containing a conflicting enum module, it could also cause this error.

Solution: as a debugging step, you can temporarily unset this variable in your terminal session to see if it resolves the issue.

# On Linux or macOS
unset PYTHONPATH

If this fixes the problem, you should permanently remove or correct the invalid path in your shell's startup file (e.g., .bashrc, .zshrc).

Conclusion

Underlying ProblemHow to DiagnoseSolution
Python version is too old (< 3.6).Check your Python version: python --version.Upgrade Python or use IntEnum as a fallback.
enum34 package is installed.Check with pip list | grep enum34.Uninstall the package: pip uninstall -y enum34.
Local file named enum.py.Check print(enum.__file__).Rename your local file to something else.
Incorrect PYTHONPATH setting.Check the value of the environment variable.Unset or correct the PYTHONPATH variable.

By systematically checking for these common issues, you can quickly identify the source of the AttributeError and ensure your environment is correctly configured to use the IntFlag class.