35

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:\Dev\ApacheCassandra\apache-cassandra-2.0.1\bin>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!

Eric Francis
  • 23,039
  • 31
  • 88
  • 122

6 Answers6

50

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.

  • Yep. should downgrade python or it's just a series of syntax errors. – Sachin Verma Oct 06 '17 at 10:14
  • @EricFrancis I downgraded to python2.7 via anaconda and switch environments with `source activate py27`. However, after I start cassandra in the py27 environment, I still get the same error when running `cqlsh`. Any advice? I'm working in Ubuntu 14.04. – d84_n1nj4 Dec 20 '17 at 20:59
  • For cqlsh version 5.0.1 (which supports only python 2.7) solution was to change cqlsh file header from ``#!/usr/bin/python3`` to ``#!/usr/bin/python`` – heroin Jun 06 '18 at 13:56
  • downloaded for python3 only, but still shows the error – SibiCoder Feb 07 '20 at 11:12
3

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.
Olukayode
  • 89
  • 2
0

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.

Himanshu
  • 106
  • 1
  • 3
0
  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 "%~dp0\cqlsh.py" %* goto finally

Prathap Kudupu
  • 1,315
  • 11
  • 11
0

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!

0

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:\Users\USER\AppData\Local\Programs\Python\Python3X-XX and rename your Python.exe to Python3.exe (The 3X-XX represent whichever Python3 version you have installed & the Users\USER would be your computer's user account)

Step 2

Go to folder C:\Users\USER\AppData\Local\Programs\Python\Python3X-XX\Scripts 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:\Users\USER\AppData\Local\Programs\Python\Python3X-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!

  • You know this is an 8-year old question, right? Anyway, I think it's bad advice to just rename installed executables. This might break other applications and/or prevent the application from uninstalling correctly. Also the installation path is not the same for everybody. – wovano Oct 16 '21 at 14:04