如何在不输入运行python脚本“巨蟒......” [英] how to run python script without typing 'python ...'

查看:149
本文介绍了如何在不输入运行python脚本“巨蟒......”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想没有明确其所称的蟒蛇在我的shell每次运行一个python脚本。我试着添加家当#!/路径/要/蟒蛇但这似乎并没有工作。有谁知道解决这个工作?非常感谢。

I want to run a python script without explicitly having to call "python" every time in my shell. I've tried to add the shebang #!/path/to/python but this does not seem to work. Does anyone know a work around this? Many thanks.

推荐答案

您已经有了添加的家当:

You've got to add the shebang:

#!/usr/bin/env python

然后使脚本可执行:

Then make the script executable:

chmod +x foo

然后就可以像运行任何其他可执行:

Then you can run it like any other executable:

./foo

和从Homer6记:如果你正在编辑从Windows文件并调用它在Linux上,你可能会碰到的神秘没有这样的文件或目录错误。这是由于被CRLF,而不是LF行的行尾。如果将它们转换为LF,预期将执行该脚本。记事本++>查看>显示符号>线显示最终以显示EOL字符。和记事本++>编辑> EOL转换> UNIX格式转换所有行结束使用LF。另外,您也可以使用 DOS2UNIX的工具( DOS2UNIX的foo.py ),这是present大多数Linux系统。

And a note from Homer6: if you're editing the file from windows and invoking it on linux, you may run into the cryptic "No such file or directory" error. It's due to the line endings of the lines being CRLF instead of LF. If you convert them to LF, the script will execute as expected. Notepad++ > View > Show Symbols > Show End of Line to show the EOL characters. And Notepad++ > Edit > EOL Conversion > Unix Format to convert all line endings to use LF. Alternatively, you can use the dos2unix tool (dos2unix foo.py), which is present on most Linux systems.

这篇关于如何在不输入运行python脚本“巨蟒......”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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