Mukund
Mukund

Reputation: 61

import error no module named kafka

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.

Upvotes: 6

Views: 38224

Answers (4)

Ogechukwu Ata
Ogechukwu Ata

Reputation: 1

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.

Upvotes: 0

Gonçalo Peres
Gonçalo Peres

Reputation: 13582

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.

Upvotes: 0

Ayoub Omari
Ayoub Omari

Reputation: 906

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)

Upvotes: 3

Eduardo Liendo
Eduardo Liendo

Reputation: 151

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

Upvotes: 13

Related Questions