在 Python 脚本的第一行使用 #!/usr/bin/env 有什么作用

发布时间 2023-10-31 16:11:38作者: 小鲨鱼2018

 

#!/usr/bin/env 告诉计算机用什么程序执行该脚本。

001、测试两个程序

[root@pc1 test]# ls                  ## 两个测试程序
test2.py  test.py
[root@pc1 test]# cat test.py         ## 程序1
print('hello world')
[root@pc1 test]# cat test2.py        ## 程序2
#!/usr/bin/env python
print('hello world')
[root@pc1 test]# python test.py       ## 运行方式1
hello world
[root@pc1 test]# python test2.py      ## 运行方式1
hello world
[root@pc1 test]# ./test.py            ## 运行方式2
./test.py: line 1: syntax error near unexpected token `'hello world''
./test.py: line 1: `print('hello world')'
[root@pc1 test]# ./test2.py           ## 运行方式2
hello world
[root@pc1 test]# python --version
Python 2.7.5

 。

 

来源:

01、https://mp.weixin.qq.com/s?__biz=MzI4NDUzNjk1Nw==&mid=2247487773&idx=1&sn=0cd2fc717ef2ad94d2af9d5b8f2d23d2&chksm=ebf8a80fdc8f21191be195d9844234cafd98cd43437b357d2d2cd00c1dd7bf56cd10705c7db9#rd