import error no module named kafka
Asked Answered
B

4

6

I am trying to import KafkaConsumer from kafka.

It says:

no module named kafka

from kafka import KafkaConsumer
import sys

consumer = KafkaConsumer('test', bootstrap_servers='10.221.129.223')

root@ubuntu-14:~/kafka_2.11-0.8.2.1# ls bin config libs LICENSE NOTICE test.py

Any suggestions? As this was working fine sometime back. After installing latest version of kafka it throws this error.

Boz answered 26/4, 2018 at 6:4 Comment(0)
R
13

Hi to fix this problem you must have to install the kafka library for python: kafka-python

Here is the command for linux/debian based OS

pip install kafka-python

I think you also have to install kafka on your OS.

https://www.digitalocean.com/community/tutorials/how-to-install-apache-kafka-on-ubuntu-14-04

Regulable answered 5/6, 2018 at 16:44 Comment(0)
P
3

I had the same issue. If Kafka and kafka-python are already installed and you are still having this problem, check the python version you are running your script with. (for e.g. kafka-python can be installed in python3 and you are trying to execute your script with python2)

Poteet answered 29/12, 2018 at 10:29 Comment(0)
R
0

The kafka library is now called Kafka Python Client (official documentation).

In order to install it, if one is using Anaconda (package page on Anaconda's official site), one can run

conda install kafka-python

Else, one can use

pip install kafka-python

For more information on how to install it, check this page.

There are other Python Clients for Kafka, such as Confluent's Python Client (official documentation).

This one is also available on PyPI so one can install it using pip as follows

pip install confluent-kafka

Notes:

  • If one is having issues with Python's installation, using the Anaconda Distribution might facilitate things out. If one is using Anaconda, it is recommended to use conda install package in the environment that one is working on, and use pip only if it is not available with conda install.

  • A while back one had pykafka but it is not maintained anymore.

Rabid answered 21/9, 2022 at 9:38 Comment(0)
E
0

I don't know if it counts, but I created a python environment and then activated it and installed it again.

python -m venv env  

then

env\Scripts\activate  

and then

pip install kafka-python
pip install kafka

After I did this it worked. This is specifically for windows.

Eaglewood answered 13/7 at 18:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.