Mac中的Shell无法运行Python
我在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 个回答
2
如果你想像这样运行你的脚本 ./test.py
,你需要在文件的最上面加一个叫做 shebang 的东西:
#!/usr/bin/env python
import subprocess
# ...
这个shebang会告诉你的系统用什么程序来执行这个脚本。你还需要让这个文件可以执行:
chmod +x ./test.py
可以查看 在Unix平台上使用Python 的相关内容。