Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python script won't run

Tags:

python

I have some scripts in the folder ~/Scripts which I have added to the path. So I tried to test if I can run them, just by calling them. I have python 3.1 over Linux Mint 11.

user@pc ~/Scripts $ python aek.py
AEK

user@pc ~/Scripts $ aek.py

/home/user/Scripts/aek.py: line 1: syntax error near unexpected token `'AEK''

/home/user/Scripts/aek.py: line 1: `print('AEK')'

The code is just this one line:

print('AEK')
like image 500
stephanos Avatar asked Dec 10 '22 06:12

stephanos


2 Answers

You need to add the very first line in your script:

#!/usr/bin/python

Or whatever interpreter you want to use. If not, the shell (probably bash) will think that it is a shell script and choke.

If you want to get the python interpreter from the path, do instead:

#!/usr/bin/env python

For extra information, see shebang.

like image 181
rodrigo Avatar answered Dec 28 '22 22:12

rodrigo


The error is not a python error but a shell error.

You should add a shebang line if you don't run them via the python executable.

And it most definitely is not a python2 <-> python3 conflict. python2 handles parens here quite well (but there are corner cases where it breaks).

like image 45
Michael Markert Avatar answered Dec 28 '22 22:12

Michael Markert