Search code examples
pythondjangoapache-kafka

ModuleNotFoundError: No module named 'kafka.vendor.six.moves' in Dockerized Django Application


I am facing an issue with my Dockerized Django application. I am using the following Dockerfile to build my application:

FROM python:alpine

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV DJANGO_SUPERUSER_PASSWORD datahub

RUN mkdir app
WORKDIR /app
COPY ./app .
RUN mkdir -p volumes

RUN apk update
RUN apk add --no-cache gcc python3-dev musl-dev mariadb-dev

RUN pip3 install --upgrade pip
RUN pip3 install -r requirements.txt

RUN apk del gcc python3-dev musl-dev

CMD python3 manage.py makemigrations --noinput &&\
    while ! python3 manage.py migrate --noinput; do sleep 1; done && \ 
    python3 manage.py collectstatic --noinput &&\
    python3 manage.py createsuperuser --user datahub  --email admin@localhost --noinput;\
    python3 manage.py runserver 0.0.0.0:8000

In my requirements.txt file:

kafka-python==2.0.2

When I run my application inside the Docker container, I encounter the following error:

ModuleNotFoundError: No module named 'kafka.vendor.six.moves'

Compelete Error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.12/site-packages/kafka/__init__.py", line 23, in <module>
    from kafka.consumer import KafkaConsumer
  File "/usr/local/lib/python3.12/site-packages/kafka/consumer/__init__.py", line 3, in <module>
    from kafka.consumer.group import KafkaConsumer
  File "/usr/local/lib/python3.12/site-packages/kafka/consumer/group.py", line 13, in <module>
    from kafka.consumer.fetcher import Fetcher
  File "/usr/local/lib/python3.12/site-packages/kafka/consumer/fetcher.py", line 19, in <module>
    from kafka.record import MemoryRecords
  File "/usr/local/lib/python3.12/site-packages/kafka/record/__init__.py", line 1, in <module>
    from kafka.record.memory_records import MemoryRecords, MemoryRecordsBuilder
  File "/usr/local/lib/python3.12/site-packages/kafka/record/memory_records.py", line 27, in <module>
    from kafka.record.legacy_records import LegacyRecordBatch, LegacyRecordBatchBuilder
  File "/usr/local/lib/python3.12/site-packages/kafka/record/legacy_records.py", line 50, in <module>
    from kafka.codec import (
  File "/usr/local/lib/python3.12/site-packages/kafka/codec.py", line 9, in <module>
    from kafka.vendor.six.moves import range
ModuleNotFoundError: No module named 'kafka.vendor.six.moves'

I have already tried updating the Kafka package, checking dependencies, and installing the six package manually. However, the issue still persists. Can anyone provide insights on how to resolve this error?

Thank you in advance for your help!


Solution

  • This appears to be a Python 3.12 issue, I have the same error but in an entirely different context.

    Instead of FROM python:alpine

    I suggest you use FROM python:3.11

    3.12 is still very new are there are many projects still trying to work out issues.