Shell无法在mac中运行python

时间:2014-09-05 09:39:59

标签: python shell

我在mac下面有一个shell脚本:

python Test.py 

和Test.py如下:

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)

我在Mac终端执行“python Test.py”,没关系。 但我执行“./Test.sh” 它会得到以下错误:

./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

1 个答案:

答案 0 :(得分:2)

如果您想运行./test.py之类的脚本,则文件顶部需要shebang

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

这将告诉你的shell应该使用哪个解释器来执行该脚本。您还需要使其可执行:

chmod +x ./test.py

请参阅Using Python on Unix platforms