15

Installed cassandra 3.11.1 version in Windows 10 with jdk 1.8.0_151 and python 3.6. All paths available in environment variables. When I run cqlsh in command prompt, I get the below error:

File "C:\Cassandra\bin\\cqlsh.py", line 145
    except ImportError, e:
                      ^
SyntaxError: invalid syntax

Anyone, Any luck with the above error?

Aaron
  • 55,518
  • 11
  • 116
  • 132
Vaibhav Gupta
  • 353
  • 1
  • 2
  • 17
  • Duplicate, Answered here https://stackoverflow.com/questions/19142231/cassandra-file-cqlsh-line-95-except-importerror-e – Prabhakar Aug 23 '18 at 05:10

12 Answers12

8

I had the same problem.I downgrade python to 2.7 and it works for me.

ERROR:

cqlsh.py", line 146 except ImportError, e: ^ SyntaxError: invalid syntax

Priyanshu Singh
  • 704
  • 8
  • 12
  • The codes (at least till Cassandra 3.11.7) are written for Python 2.x and it will be wise to downgrade to Python 2.7 rather than fixing the syntax. There are more than just line 146 of cqlsh.py that would need a fix, if you venture to make cqlsh work with python 3.x (I tried 3.8). – KGhatak Aug 17 '20 at 07:44
  • I too had same problem and after installing Python 2.7 problem fixed :) – Madhusudan Sep 11 '20 at 05:22
3

cqlsh requires a Python 2 interpreter. See https://issues.apache.org/jira/browse/CASSANDRA-10190

kermatt
  • 1,585
  • 2
  • 16
  • 36
  • Does that mean cqlsh won't be able to run for python 3.5 or python 3.6? – Vaibhav Gupta Jan 16 '18 at 02:49
  • 1
    In its current form, only Python 2 is supported. The Jira issue above is to provide Python 3 support. – kermatt Jan 16 '18 at 18:11
  • I updated cqlsh but now I am getting the below new error: "'\nWarning: Specified cqlshrc location %s does not exist. Using %s instead.\n' % (CONFIG_FILE, HISTORY_DIR) ^ SyntaxError: invalid syntax" – Vaibhav Gupta Jan 19 '18 at 11:18
  • 2
    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
1

Ref: https://docs.python.org/3.6/tutorial/errors.html

The error is correct as your syntax is wrong. It should be:

except ImportError as e:
Simon Fontana Oscarsson
  • 2,114
  • 1
  • 17
  • 20
  • I have modified my cqlsh.py file but now I am getting a new error: print '\nWarning: Specified cqlshrc location `%s` does not exist. Using `%s` instead.\n' % (CONFIG_FILE, HISTORY_DIR) ^ SyntaxError: invalid syntax – Vaibhav Gupta Jan 12 '18 at 19:52
  • Refer to @kermatt's answer, this will not solve the problem as the cqlsh package only supports Python2 for now. If you fix this line, there will be other errors and warnings. – Osama Dar Jul 30 '20 at 08:01
1

My case: I had to install Apache cassandra. I already had Python3 installed in my D: drive. With loads of development work in process on Python 3, i didn't wanted to mess my Python 3 installation. And, i needed Python2 only for Apache cassandra.

You came here looking for answer, so:

  • I assume you have all prequisites installed for Apache cassandra, except Python2.
  • You did not installed Python2 because you already have Python3 up & running on your system.
  • You are getting error when you are trying to run cqlsh
  • You don't want to install Python2 because you don't want to mess up anything in/on already installed Python3.

Well i had same issues. Now, check my answer below.

https://stackoverflow.com/a/58285774/1513779

Advice:

  1. Don't try except ImportError as e: in cqlsh.py as suggested above. You will be caught in loop of numerous errors & warnings. Just install Python2, following steps mentioned in link.
Manjeet
  • 949
  • 14
  • 23
1

The reason is because your Cassandra is referencing Python 3 installed on your system. To fix this, do the following..,

  1. Install python 2 on your system.
  2. Add python2 to your environment variables.
  3. Navigate to your Cassandra bin folder eg C:\Cassandra\apache-cassandra-3.11.9\bin>
  4. py -2 -V (verify that you have python 2 installed)
  5. py -2 -m cqlsh (from your Cassandra bin folder.)
Olukayode
  • 89
  • 2
0

In case you have different versions of python I suggest this:

  • Install cqlsh using

    python2.7 -m pip install cqlsh

This will install cqlsh in the context of your Python2.7

  • Also you might need to install cassandra-driver as well the same way:

    python2.7 -m pip install cassandra-driver

Rafael
  • 2,521
  • 2
  • 33
  • 59
0

You can install python2 alongside with python3, and change the cqlsh.bat file in bin folder from python to python2 like this

@echo off

if "%OS%" == "Windows_NT" setlocal

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

python2 "%~dp0\cqlsh.py" %*
goto finally

:err
echo Can't detect Python version!

:finally

ENDLOCAL

Note: To install python3 and python2, you can add both to path, and change python.exe in folder python2 into python2.exe

0

Better to hard code PATH in the batch/shell file 'cqlsh'. Or change the directory to the Python2 installed directory, like I did in the below code.

if "%OS%" == "Windows_NT" setlocal

cd C:\Python27
python -V >nul 2>&1
if ERRORLEVEL 1 goto err
Indrajeet4192
  • 51
  • 2
  • 9
0

Simply install python 2.7 and add it to the path. This worked for me! Download python 2.7 from here Python official download page

Add location of python 2.7 to environment variablesEnvironment variable setup Next finally

Jane Kathambi
  • 695
  • 6
  • 8
-1

I had the same problem as you. I had installed python 2.7 version as mentioned at Prerequisites and the error no longer appears

afshar
  • 523
  • 4
  • 16
-2

First of all install python 2.7 set path in system variable C:\Python27 in PATH key

-2

If you have Cassandra 4.0 installed use sudo cqlsh. It will work without any errors

  • 1
    This doesn't really answer the asked question, though, which was created before Cassandra 4.0 was released. Plus, you don't need sudo for a cqlsh session – OneCricketeer Oct 20 '20 at 05:36