Blue Moon
Blue Moon

Reputation: 4661

syntax error near unexpected token `(' python script

I am running a python code that has to interact between a windows machine and a linux machine.

The code is launched on windows, the computations are performed by the server, and the result comes back in a folder on windows.

When I run the code on my Windows machine is fine but when this is passed through the linux server I get the following error message:

 line 25: syntax error near unexpected token `('
 line 25: `db = MySQLdb.connect(host="192.168.1.18", # host

while the line of code is:

 db = MySQLdb.connect(host="192.168.1.18", # host

What does the error message mean and how to solve it?

Thanks

Upvotes: 1

Views: 14952

Answers (2)

bad programmer
bad programmer

Reputation: 934

When executing python script in a remote server , you need to create .sh file , give location of python script inside that .sh file. then execute the sh file.

The whole process looks like :

  • create a .sh file using some editor (vi,echo) i.e. vi any_name.sh
  • inside the sh file . copy paste

    #!/usr/bin/env python

    python /home/..../script_folder/your_script_name.py

  • Now save and quit the editor, and enter command : ./any_name.sh

It's because while executing python code in servers , they mostly accept .sh only. Inside the .sh file , we tell computer to execute the python script using the python inerpreter.

Upvotes: 1

Andrea Corbellini
Andrea Corbellini

Reputation: 17761

The Python file needs to be executed by the Python interpreter.

You can do e.g.:

python script.py

where script.py is the name of your file.

What you are doing instead is running your Python script through Bash (in fact, what you are getting is a typical Bash error). Probably this is happening because you are using ./script.py, but your script is missing the correct shebang line:

#!/usr/bin/env python

Indeed, if yours is a Python 3 script, you should use python3 instead of python.

Upvotes: 7

Related Questions