Python
AttributeError
module enum
IntFlag
Python 3.6.1

Why Python 3.6.1 throws AttributeError module 'enum' has no attribute 'IntFlag'?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The error AttributeError: module 'enum' has no attribute 'IntFlag' occurs when Python imports a third-party package named enum instead of the standard library's enum module. This typically happens because a pip-installed package called enum34 (a backport for Python 2) shadows the built-in enum module, or because a file named enum.py exists in your project directory. The fix is to uninstall enum34 and remove any local enum.py file.

The Error

python
import re  # or any module that uses enum internally

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

IntFlag was added to the standard library enum module in Python 3.6. If Python finds a different enum module first (one that does not have IntFlag), this error occurs.

Fix 1: Uninstall enum34

The most common cause is having the enum34 package installed:

bash
pip uninstall enum34

enum34 is a backport of the enum module for Python 2 and Python 3.3 or earlier. It should NOT be installed on Python 3.4+, as it shadows the built-in enum module which has more features (including IntFlag).

Check if enum34 is installed

bash
1pip show enum34
2# If installed, it shows version info
3# If not installed, shows "WARNING: Package(s) not found: enum34"
4
5pip list | grep enum
6# Look for "enum34" in the output

Fix 2: Remove Local enum.py File

Check if you have a file named enum.py in your project or current directory:

bash
1# Check for local enum.py
2ls enum.py
3ls enum.pyc
4
5# On Linux/macOS
6find . -name "enum.py" -not -path "*/lib/*"
7
8# Remove it (or rename it)
9mv enum.py my_enums.py

Also check for a compiled __pycache__/enum.cpython-*.pyc file and delete it:

bash
find . -name "enum.cpython*" -delete

Fix 3: Check import Order

Verify which enum module Python is loading:

python
1import enum
2print(enum.__file__)
3
4# Expected (standard library):
5# /usr/lib/python3.6/enum.py
6# or
7# /usr/local/lib/python3.6/enum.py
8
9# Problem (third-party):
10# /usr/local/lib/python3.6/site-packages/enum/__init__.py
11# or
12# ./enum.py (local file)

If the path points to site-packages or your local directory, that is the wrong module.

Fix 4: Fix in Virtual Environment

bash
1# Create a clean virtual environment
2python3 -m venv clean_env
3source clean_env/bin/activate  # Linux/macOS
4# or: clean_env\Scripts\activate  # Windows
5
6# Verify enum is from stdlib
7python -c "import enum; print(enum.__file__)"
8
9# Install your dependencies (without enum34)
10pip install -r requirements.txt

Fix 5: Remove enum34 from requirements.txt

Check your requirements.txt or setup.py for enum34:

 
1# requirements.txtREMOVE this line:
2enum34==1.1.10
3
4# Or make it conditional (only install on Python < 3.4):
5# In setup.py:
6install_requires=[
7    'enum34; python_version < "3.4"',
8]

Some older packages list enum34 as a dependency. If removing it breaks another package, update that package to a newer version that does not require enum34.

Understanding Python's enum Module

python
1import enum
2
3# Flag (Python 3.6+)
4class Permission(enum.Flag):
5    READ = 1
6    WRITE = 2
7    EXECUTE = 4
8
9# IntFlag (Python 3.6+) — supports bitwise AND, OR with integers
10class FilePermission(enum.IntFlag):
11    READ = 4
12    WRITE = 2
13    EXECUTE = 1
14
15# Can combine with integers
16combined = FilePermission.READ | FilePermission.WRITE  # FilePermission.READ|WRITE
17print(int(combined))  # 6
18
19# IntEnum (Python 3.4+)
20class Priority(enum.IntEnum):
21    LOW = 1
22    MEDIUM = 2
23    HIGH = 3
24
25# Comparable with integers
26print(Priority.HIGH > 2)  # True

IntFlag extends Flag and int, allowing both flag-style combinations and integer comparisons. It was added in Python 3.6 and is not available in the enum34 backport.

Debugging Checklist

bash
1# 1. Check Python version (must be 3.6+)
2python --version
3
4# 2. Check if enum34 is installed
5pip list | grep enum
6
7# 3. Check which enum module is loaded
8python -c "import enum; print(enum.__file__); print(dir(enum))"
9
10# 4. Check for local enum.py files
11find . -name "enum.py" 2>/dev/null
12
13# 5. Check sys.path for unexpected directories
14python -c "import sys; print('\n'.join(sys.path))"

Common Pitfalls

  • Installing enum34 on Python 3.6+: enum34 overwrites the standard library's enum module with an older version that lacks IntFlag, Flag, and auto(). Never install enum34 on Python 3.4 or later.
  • A dependency pulling in enum34: Some older packages declare enum34 as a dependency unconditionally. Check pip show <package> for your installed packages to find which one requires enum34, and update or replace it.
  • Local enum.py or enum/ directory: Python searches the current directory before the standard library. A file named enum.py or a directory named enum/ in your project shadows the built-in module.
  • Cached .pyc files: After removing a local enum.py, the compiled __pycache__/enum.cpython-*.pyc may still be loaded. Delete the __pycache__ directory or the specific .pyc file.
  • Conda environments: In Conda, enum34 can be installed through Conda channels even on Python 3.6+. Use conda list | grep enum and conda remove enum34 to fix.

Summary

  • Uninstall enum34 with pip uninstall enum34 — it shadows the standard library on Python 3.4+
  • Remove any local enum.py file or enum/ directory from your project
  • Check which enum module is loaded with import enum; print(enum.__file__)
  • IntFlag is part of the standard library enum module since Python 3.6
  • Remove enum34 from requirements.txt and use conditional dependencies if needed for backward compatibility

Course illustration
Course illustration

All Rights Reserved.