且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

运行没有 "python" 的 python 脚本关键词

更新时间:2023-08-21 18:35:10

添加shebang:

#!/usr/bin/python

#!/usr/bin/env python

我更喜欢第二个,因为 Python 可以在任何地方,如/usr/bin/python、/usr/local/bin/python 等,第二个确保您不必继续编辑shebang.>

然后你可以将它作为 ./script.py 执行,如果它是可执行的.

How can I run a python script in Terminal on Mac without using the "python" keyword, without having to edit my existing python files?

Right now I have to do this: python script.py

What I like to do is this: script.py

Add a shebang:

#!/usr/bin/python

or

#!/usr/bin/env python

I prefer the second one, since Python can be anywhere like /usr/bin/python, /usr/local/bin/python etc. and second one ensure that you don't have to keep editing the shebang.

And then you can just execute it as ./script.py if it is executable.