-2

I have a shell script in mac below:

python Test.py 

and Test.py is below:

import subprocess
import os.path
from os import listdir
from os.path import isfile, isdir, join
from filecmp import dircmp
import json
import sys
import shutil
....(skip)

and I execute "python Test.py" in Mac Terminal,it's ok. but I execute "./Test.sh" it will get error below:

./Test.py: line 1: import: command not found
./Test.py: line 2: import: command not found
from: can't read /var/mail/os
from: can't read /var/mail/os.path
from: can't read /var/mail/filecmp
./Test.py: line 6: import: command not found
./Test.py: line 7: import: command not found
./Test.py: line 8: import: command not found
敬錞 潘
  • 852
  • 1
  • 14
  • 29
  • Why did you edit your question to say `/Test.sh`? From your output it's obvious that this isn't what you're actually running. – Lukas Graf Sep 05 '14 at 10:12

1 Answers1

2

If you want to run your script like ./test.py, you need a shebang at the top of the file:

#!/usr/bin/env python
import subprocess
# ...

This will tell your shell what interpreter it should use to execute that script. You'll also need to make it executable:

chmod +x ./test.py

See Using Python on Unix platforms.

Lukas Graf
  • 30,317
  • 8
  • 77
  • 92