1

I am trying to install Kafka-Python in my MacBook but unable to do so. Below is the version of software installed on my MacBook.

Mac Sierra
Python 3.7.0a4
Python 2.7.10(Default)
Kafka 3.5

First I used the command sudo easy_install pip to install pip.

Next I tried using pip install kafka-python to install Kafka-python. But it failed because there are 2 Kafka instances running and it used the older version(2.7). Below is the error I got:

Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_set.py", line 784, in install
    **kwargs
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/wheel.py", line 345, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/wheel.py", line 316, in clobber
    ensure_dir(destdir)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/utils/__init__.py", line 83, in ensure_dir
    os.makedirs(path)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/kafka_python-1.3.5.dist-info'

I wanted to install Kafka-python on python 3.7 version but the command again and again used the version 2.7. So finally I followed the below URL to install pip3 followed by Kafka-python and it got installed successfully: http://itsevans.com/install-pip-osx/ .

Post-installation, pip3 is installed in path /Library/Frameworks/Python.framework/Versions/3.7/bin/pip3. And Kafka-Python is installed in path /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages.

The folders in the path are :

-rw-rw-r--   1 root     admin   126 Jan 24 20:52 easy_install.py
drwxrwxr-x  34 root     admin  1088 Jan 24 20:52 setuptools
drwxrwxr-x   6 root     admin   192 Jan 24 20:52 pkg_resources
drwxrwxr-x  12 root     admin   384 Jan 24 20:52 setuptools-28.8.0.dist-info
drwxrwxr-x  23 root     admin   736 Jan 24 20:52 pip
drwxrwxr-x  10 root     admin   320 Jan 24 20:52 pip-9.0.1.dist-info
drwxrwxr-x   4 root     admin   128 Jan 24 20:52 __pycache__
drwxr-xr-x  18 *******  admin   576 Feb  2 12:26 wheel
drwxr-xr-x  11 *******  admin   352 Feb  2 12:26 wheel-0.30.0.dist-info
drwxr-xr-x  24 *******  admin   768 Feb  2 12:28 kafka
drwxr-xr-x   9 *******  admin   288 Feb  2 12:28 kafka_python-1.3.5.dist-info

when I try to import Kafka in terminal or in PyCharm, it does not recognize the Kafka package at all and gives compilation error. In terminal it throws below error:

>>> import kafka
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kafka/__init__.py", line 23, in <module>
    from kafka.producer import KafkaProducer
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kafka/producer/__init__.py", line 4, in <module>
    from .simple import SimpleProducer
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/kafka/producer/simple.py", line 54
    return '<SimpleProducer batch=%s>' % self.async
                                                  ^
SyntaxError: invalid syntax

The Pycharm editor simply does not recognize the Kafka package and hence gives compilation error. Any help on how to configure Kafka-python is greatly appreciated.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Chandan
  • 11
  • 1
  • 3
  • Is this related to this? https://github.com/dpkp/kafka-python/issues/1451 – dantibb Apr 21 '18 at 14:09
  • Does this answer your question? [SyntaxError on "self.async" when running python kafka producer](https://stackoverflow.com/questions/65809459/syntaxerror-on-self-async-when-running-python-kafka-producer) – OneCricketeer Feb 01 '23 at 07:18

2 Answers2

3

It seems that python3.7 breaks the compatibility. The latest version of kafka-python (1.4.3) fixes this issue : "Fix for python 3.7 support: remove 'async' keyword from SimpleProducer (dpkp #1454)"

Source : https://github.com/dpkp/kafka-python/releases/tag/1.4.3

VincentM
  • 126
  • 5
0

I don't know if you fixed this problem or not. But this answer helps to resolve this issue.

Git hub link

Edit: Basically, the problem is with the Kafka package which uses an older version. You need to install the current version to resolve this issue. Try to install 'kafka-python' package using pip cmd. This works for me, hope will work for you too.

Jay Parmar
  • 11
  • 3
  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](https://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers/8259#8259) so your fellow users will have some idea what it is and why it's there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted](https://stackoverflow.com/help/deleted-answers). Also read [How to Answer](https://stackoverflow.com/help/how-to-answer). –  Jan 16 '21 at 20:09
  • 1
    Thanks, @tibetiroka for pointing this. I am new to this. I edited my answer hope it will be fine. – Jay Parmar Jan 16 '21 at 20:45