如何在Linux中执行python文件 [英] How to execute python file in linux
问题描述
我正在使用linux mint,并且要运行python文件,我必须在终端中键入:python [file path]
,所以有办法使该文件可执行,并使其在我doublr时自动运行python
命令.单击它吗?
I am using linux mint, and to run a python file I have to type in the terminal: python [file path]
, so is there way to make the file executable, and make it run the python
command automatically when I doublr click it?
而且由于我很早以前就停止处理Windows,所以我想知道那里的.py文件是否也可以自动执行,还是我需要一些步骤.
And since I stopped dealing with windows ages ago, I wonder if the .py files there are also automatically executable or do I need some steps.
谢谢
推荐答案
您必须添加一个shebang. shebang是文件的第一行.这是系统为了执行文件所要查找的内容.
You have to add a shebang. A shebang is the first line of the file. Its what the system is looking for in order to execute a file.
它应该看起来像这样:
#!/usr/bin/env python
或真实路径
#!/usr/bin/python
您还应该检查文件是否有权执行. chmod +x file.py
You should also check the file have the right to be execute. chmod +x file.py
正如Fabian所说,请看一下Wikipedia: Wikipedia-Shebang(zh-CN)
As Fabian said, take a look to Wikipedia : Wikipedia - Shebang (en)
这篇关于如何在Linux中执行python文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!