Cassandra: File "cqlsh", line 95 except ImportError, e:

Question:

I am having trouble getting Cassandra up and running.
I have downloaded Cassandra 2.0.1 and Python 3.3.2.

Upon starting the CLI for cassandra I get an error:

C:DevApacheCassandraapache-cassandra-2.0.1bin>python cqlsh
  File "cqlsh", line 95
    except ImportError, e:
                      ^
SyntaxError: invalid syntax

Any suggestions? I am going to downgrade python to 2.7 and see if that fixes my issue.

Thanks!

Asked By: Eric Francis

||

Answers:

The version of Cassandra that you are using is only compatible with Python 2.x.

The following syntax:

except ImportError, e:

was deprecated in Python 2.7 and removed in Python 3.x. Nowadays, you use the as keyword:

except ImportError as e:

This means that you need to either downgrade to Python 2.x or get a version of Cassandra that is compatible with Python 3.x.

Answered By: user2555451

If anybody still looking for an answer, the best method is provided in the comment in the above answer by @heroin i.e. in the cqlsh file, change the header from current python3 interpreter to a python2 interpreter.
e.g.
Old

#!/usr/bin/python3

Modified

#!/usr/bin/python2

Check the path and name of your python2 interpreter and replace above. Now cqlsh will use python2 and run without any issue.

Answered By: Himanshu
  1. You need to have pyhon2 installed.
  2. Set the path variable in environment variables
  3. Rename python.exe to python2.exe In cqlsh.py
  4. change the script in cqlsh to (python to python2)

enter image description here
if “%OS%” == “Windows_NT” setlocal

python2 -V >nul 2>&1
if ERRORLEVEL 1 goto err

python2 “%~dp0cqlsh.py” %*
goto finally

Answered By: Prathap Kudupu

You should install python 2 and add it to your environment variable as stated above

  1. py -2 -V(verify that you have python 2 installed)
  2. py -2 -m cqlsh (from your Cassandra bin folder.)
  3. View image here.
Answered By: Olukayode

I am not an expert but I can share what worked best for me without worrying about downgrading python to version 2 for the entire system.

  1. Install Anaconda
  2. Go to the environment tab at the right column
  3. At the bottom there is an option to create.
  4. For the packages of python please select 2.7. Name anything you want and then click create
  5. Now you can click on the environment you have created and open terminal for it
  6. Finally run command of cqlsh.

Everything should be working fine now!

Answered By: Sudhanshu Pandey

In Windows 10 this worked for me. For starters please note that I had both Python 3 and Python 2 installed in my computer prior to encountering this error

Step 1

Go to folder C:UsersUSERAppDataLocalProgramsPythonPython3X-XX and rename your Python.exe to Python3.exe (The 3X-XX represent whichever Python3 version you have installed & the UsersUSER would be your computer’s user account)

Step 2

Go to folder C:UsersUSERAppDataLocalProgramsPythonPython3X-XXScripts and rename your pip.exe to pip3X.exe (use the same 3X which you have in the path)

Step 3

Go to your Python 2 folder (mine was at C:Python27) and make sure that you have a exe file called python.exe

Step 4

Now go to your Environment Variables (Lower Right Button) under System Properties > Advance Tab. Once inside, double click the path under the System variables section. This will open up the list of paths that are in your system. Add both Python paths by clicking new and then browse (selecting first (C:Python27) or whatever you Python 2 path is) and then by adding the Python 3 Path (C:UsersUSERAppDataLocalProgramsPythonPython3X-XX).

Hit Ok on all screens and try running CQLSH or CQLSH 192.168…. (whatever the IP is of your seed node) via cmd and it should work!

Answered By: Waqas Ahmed
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.