ModuleNotFoundError: No module named kafka.vendor.six.moves in Dockerized Django
Replace unmaintained kafka-python with the actively maintained drop-in replacement kafka-python-ng or install six directly in requirements.txt.
Root Cause Analysis
This error occurs when Python imports the legacy kafka-python library inside a Dockerized Django application on modern Python releases (3.12+), and kafka-python fails to resolve its vendored six.moves compatibility module.
1. Abandoned Vendored six in Legacy kafka-python
The original kafka-python package (version 2.0.2) has not been updated since 2020. It relies on an internal vendored version of six located at kafka.vendor.six. Under modern Python import machinery, accessing kafka.vendor.six.moves fails.
2. Python 3.12 Standard Library Import Hook Changes
Changes in Python 3.12 standard library module loaders broke custom sys.meta_path hooks that six.moves used to dynamically proxy standard library modules.
3. Docker Container Rebuilds Pulling Fresh Dependencies
When rebuilding Docker images without lockfiles, upgrading base Python images from 3.11 to 3.12 suddenly causes existing kafka-python code to crash on startup.
4. Modern Maintained Fork: kafka-python-ng
The open-source community created kafka-python-ng, a 100% API-compatible fork that removes broken vendored shims and supports Python 3.12+ and 3.13.
Reproduction Code (MCVE)
# **Note de reproductibilité :** Lié à l'environnement Docker et aux versions de paquets tiers.
raise ModuleNotFoundError("No module named 'kafka.vendor.six.moves'")
Solution 1: Switch to kafka-python-ng in requirements.txt
Replace kafka-python with kafka-python-ng in your requirements.txt or Dockerfile without changing any application import statements.
print('In requirements.txt:')
print('# Remove: kafka-python==2.0.2')
print('kafka-python-ng>=2.2.2')
print('\nIn your Python code, keep standard imports:')
print('from kafka import KafkaProducer, KafkaConsumer')
Solution 2: Use confluent-kafka for High-Performance Production
Migrate to confluent-kafka (librdkafka C bindings) for high-throughput enterprise Django deployments.
print('Alternative enterprise driver:')
print('pip install confluent-kafka')
print('from confluent_kafka import Producer, Consumer')
A common mistake is attempting to monkey-patch sys.modules['kafka.vendor.six.moves'] = six.moves in Django's manage.py. While this may bypass the initial import, internal producer serialization sub-modules will fail during background thread execution. Always switch to kafka-python-ng. Edge cases occur with Celery and Django Signals: ensure Kafka producer instances are initialized lazily to avoid socket sharing across forked processes. Contrast this error with KafkaError: NoBrokersAvailable, which indicates connection failures to the Kafka broker cluster.