简体   繁体   English

运行没有“python”关键字的 python 脚本

[英]Run python script without the “python” keyword

How can I run a python script in Terminal on Mac without using the "python" keyword, without having to edit my existing python files?如何在 Mac 上的终端中运行 python 脚本而不使用“python”关键字,而无需编辑我现有的 python 文件?

Right now I have to do this: python script.py现在我必须这样做: python script.py

What I like to do is this: script.py我喜欢做的是: script.py

in your python script add this as your first line在你的 python 脚本中添加这个作为你的第一行

#!/usr/bin/python

then in terminal do this chmod +x./yourpythonscript.py然后在终端做这个 chmod +x./yourpythonscript.py

then from terminal execute as./yourpythonscript.py然后从终端执行 as./yourpythonscript.py

Add a shebang:添加一个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.我更喜欢第二个,因为 Python 可以位于 /usr/bin/python、/usr/local/bin/python 等任何位置,第二个确保您不必继续编辑 shebang。

And then you can just execute it as ./script.py if it is executable.然后你可以将它作为./script.py执行,如果它是可执行的。

Try./script.py instead of script.py... or ensure your current directory is in your path and script.py should work....尝试./script.py 而不是 script.py... 或确保您的当前目录在您的路径中并且 script.py 应该可以工作...。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM