bash: syntax error near unexpected token `(' – Python

Question:

# from lxml import etree; 
import module2dbk; 
print module2dbk.xsl_transform(etree.parse('test-ccap/col10614/index.cnxml'), []);

Error: bash: syntax error near unexpected token `('
Asked By: Jessica Burnett

||

Answers:

add #!/usr/bin/env python at the top of your script, or call your script using python myscript.py

Answered By: zmo

Are you typing this at the unix command prompt? You should be doing this inside the python environment, ie. type python at the prompt and work from there.

Also, no ; needed at the end of the line in Python

Answered By: Levon

add

#!/usr/bin/env python

or but i will prefer to use the above one.

#!/usr/bin/python

In case you have installed python 2 and python 3 and python 2 is default you can run python 3 by using these command

#!/usr/bin/env python3

at top of the file

or run this way

python code.py
Answered By: Abdul Rehman Janjua

Well I had exactly the same problem. I had tried everything and nothing really worked. My program was running perfectly on Windows command prompt, and on my iPhone Python app interpreter, but not on my Macbook’s terminal, where I always got the following error whenever I tried to run the program:

bash: syntax error near unexpected token `(‘

Finally the comment above from the user tripleee helped me come up with a solution; although his solution of adding !/usr/bin/python at the very start of my code didn’t do it for me it helped me understand as he wrote that:

The error message indicates that the script gets executed by bash, not python.

Then I noticed that my code(extra).py contained ‘(‘ apostrophes, I renamed to my codeextra.py and that was it, problem solved. 🙂

Answered By: ManObit
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.