运行没有“python"的python脚本关键词 [英] Run python script without the "python" keyword

查看:32
本文介绍了运行没有“python"的python脚本关键词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Mac 上的终端中运行 python 脚本而不使用python"关键字,而无需编辑现有的 python 文件?

现在我必须这样做:python script.py

我喜欢做的是:script.py

解决方案

添加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.

这篇关于运行没有“python"的python脚本关键词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆