ModuleNotFoundError: No module named 'cassandra'
Asked Answered
T

3

5

After installing cassandra driver by running the command: sudo pip3 install cassandra-driver, I am getting the error ModuleNotFoundError: No module named 'cassandra' when I try to import the module by running the line cassandra.

I then tried to see what all modules are installed in pip3 by running the command pip3 freeze:

astroid==2.1.0
cassandra-driver==3.16.0
isort==4.3.4
lazy-object-proxy==1.3.1
mccabe==0.6.1
pylint==2.2.2
six==1.12.0
wrapt==1.10.11

Seeing no cassandra, I tried to import the visible module: cassandra-driver and then I ended up with the error:

File "<stdin>", line 1
    import cassandra-driver
                    ^
SyntaxError: invalid syntax

Also, when I do correct the hyphen issue with this: __import__("cassandra-driver"), I get the error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cassandra-driver'

My which python3 is: /usr/local/bin/python3 and my which pip3 is: /usr/local/bin/pip3

My OS is MacOS

How to install cassandra? Note: I am following this documentation.

Tenorite answered 12/1, 2019 at 19:15 Comment(5)
Windows or linux? Have you checked PYTHONHOME and PYTHONPATH?Aspirate
@Aspirate No, I have not checked the aforementioned variables. How does it matter though? Also, did you notice that I have already specified the python's paths; thanks for pointing out the OS, I have updated the question,Tenorite
Do you have other versions of python installed? Try which -a python and which -a pip3Aspirate
which -a python or which -a python3?Tenorite
@Aspirate which -a python gives: /usr/local/bin/python and /usr/bin/python whereas which -a pip3 gives /usr/local/bin/pip3; also, which -a python3 gives /usr/local/bin/python3.Tenorite
A
2

Did you try to run these demos (from those docs)?

"If successful, you should be able to build and install the extension (just using setup.py build or setup.py install) and then use the libev event loop by doing the following:"

>>> from cassandra.io.libevreactor import LibevConnection
>>> from cassandra.cluster import Cluster

>>> cluster = Cluster()
>>> cluster.connection_class = LibevConnection
>>> session = cluster.connect()

There is a probability that actual module is named differently, e.g. there is another external package called Pillow, but you import it with name "PIL". In docs they are importing from cassandra.cluster

Docs I'm referring to

Assembly answered 12/1, 2019 at 19:42 Comment(0)
S
2
$ echo 'import cassandra.cluster' > cassandra.py && python3 cassandra.py
Traceback (most recent call last):
  File "./cassandra.py", line 3, in <module>
    import cassandra
  File "/home/xxx/cassandra.py", line 4, in <module>
    import cassandra.cluster
ModuleNotFoundError: No module named 'cassandra.cluster'; 'cassandra' is not a package

Using a different filename, the error disappears:

echo 'import cassandra.cluster' > tmp.py && python3 cassandra.py

So, for me, the error was that my own program overrode the package. O.o

Sheronsherourd answered 19/5, 2021 at 9:28 Comment(0)
F
2

I ran into a similar issue while trying to get the cassandra-driver module installed on a Gitpod environment. The problem was the use of the sudo command:

sudo pip install cassandra-driver

I omitted the sudo command and ran pip as a user:

pip install cassandra-driver

This allowed the cassandra-driver module to be made available to the current user and resolved the issue for me.

Furious answered 2/11, 2023 at 12:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.