1

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

2
  • 3
    It looks like you aren't actually running your Python code as Python... Commented Aug 27, 2015 at 9:32
  • the closing parenthesis is in the next line of code Commented Aug 27, 2015 at 9:32

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried but now I get: /home/scripts/running_jobs/job_2953875.sh: line 5: /home/scripts/pyscripts/Carriers_Weekly_Breakdown_Comparison.py: Permission denied
I guess that you have chosen to add the shebang line and I guess that Carriers_Weekly_Breakdown_Comparison.py is missing the executable bit. If this is correct, then chmod +x Carriers_Weekly_Breakdown_Comparison.py.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.