提前感谢您的反馈!
我有一个简单的hello.py文件,我正在命令行中探索。
当我执行以下命令时:
python hello.py
我得到一个期望的输出:
Hello World
但是当我用Execute Bit命令
运行它时chmod +x hello.py
./hello.py
得到以下输出:
('Hello', 'World')
我不明白为什么第二个输出被括在括号中,并显示为单独引用的字符串。我的python版本是python 3.8.5。我的shell是zsh
hello.py文件如下:
import sys
# Define a main() function that prints a little greeting.
def main():
# Get the name from the command line, using 'World' as a fallback.
if len(sys.argv) >= 2:
name = sys.argv[1]
else:
name = 'World'
print ('Hello', name)
# This is the standard boilerplate that calls the main() function.
if __name__ == '__main__':
main()
为了能够仅使用其名称运行脚本,该文件需要有一个有效的shebang行。您没有显示您的脚本,但显然您的脚本有一个,否则shell将尝试将其作为shell脚本运行。不幸的是,你的shebang指向Python 2,而你希望它指向Python 3。
在许多Linux系统上,#!/usr/bin/python
(或道德上等价但更灵活的#!/usr/bin/env python
)将运行Python 2,而#!/usr/bin/python3
(或#!/usr/bin/env python3
)将运行Python 3。
命令行上的python
是否运行Python 3基本上是无关紧要的;这可能是您的shell设置的一个交互式别名,但是只有可以交互地工作。