【发布时间】:2019-03-28 21:24:21
【问题描述】:
这是一个棘手的问题。它有几个主题。但他们都没有帮助我。
我添加了#!/usr/bin/env python3(或python),然后运行test.py,它报告了zsh: command not found: test.py。我很困惑。我尝试过多种形式的shebang。你能帮帮我吗?
在下面的错误报告中,您可以看到在 HOME 路径和 test.py 的父路径下运行时报告不同
[Scripts] test.py 20:51:04
zsh: command not found: test.py
[Scripts] cd ~ 20:51:33
[~] Scripts/test.py 20:51:43
env: python\r: No such file or directory
没多久我就明白了shebang线的意思了。我希望它能让我的生活变得轻松,永远不要在test.py之前写python。
下面是测试代码。
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser(description='test')
parser.add_argument('-o', dest='what', action='store', default='hello', metavar='WHAT')
args = parser.parse_args()
print(args.what)
以下是配置。
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:$PATH"
在终端中,
[~] which python 20:36:55
python: aliased to python3
[~] which python3 20:36:57
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
ls -l
-rwxrwxrwx@ 1 william staff 273 10 24 20:51 test.py
【问题讨论】:
-
试试
python3 test.py而不是test.py? -
@cryptnome:shebang 应该翻译成这样
-
./test.py怎么样 -
确保脚本有执行权限并使用命令
./test.py运行 -
听起来
test.py不在您的路径中。按照其他人的建议,尝试使用相对(或绝对)路径。