我在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
答案 0 :(得分:2)
如果您想运行./test.py
之类的脚本,则文件顶部需要shebang:
#!/usr/bin/env python
import subprocess
# ...
这将告诉你的shell应该使用哪个解释器来执行该脚本。您还需要使其可执行:
chmod +x ./test.py