如何在linux中执行python文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13933169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:04:29  来源:igfitidea点击:

How to execute python file in linux

pythonlinux

提问by

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 pythoncommand automatically when I doublr click it?

我正在使用 linux mint,要运行一个 python 文件,我必须在终端中输入:python [file path],那么有没有办法让文件可执行,并让它python在我双击时自动运行命令?

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.

而且由于我很久以前就停止处理 Windows 了,我想知道那里的 .py 文件是否也可以自动执行,或者我是否需要一些步骤。

Thanks

谢谢

采纳答案by ykatchou

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.

你必须添加一个shebang。shebang 是文件的第一行。它是系统为了执行文件而寻找的。

It should look like that :

它应该是这样的:

#!/usr/bin/env python

or the real path

或真正的道路

#!/usr/bin/python

You should also check the file have the right to be execute. chmod +x file.py

您还应该检查文件是否具有执行权限。 chmod +x file.py

As Fabian said, take a look to Wikipedia : Wikipedia - Shebang (en)

正如 Fabian 所说,看看维基百科:维基百科 - Shebang (en)

回答by Max

yes there is. add

就在这里。添加

#!/usr/bin/env python

#!/usr/bin/env python

to the beginning of the file and do

到文件的开头并执行

chmod u+rx <file>

chmod u+rx <file>

assuming your user owns the file, otherwise maybe adjust the group or world permissions.

假设您的用户拥有该文件,否则可能会调整组或世界权限。

.py files under windows are associated with python as the program to run when opening them just like MS word is run when opening a .docx for example.

Windows 下的 .py 文件与 python 相关联,作为打开它们时运行的程序,就像打开 .docx 时运行 MS word 一样。

回答by Fabian

Add this at the top of your file:

在文件顶部添加:

#!/usr/bin/python

This is a shebang. You can read more about it on Wikipedia.

这是一个shebang。您可以在Wikipedia上阅读更多相关信息。

After that, you must make the file executable via

之后,您必须通过以下方式使文件可执行

chmod +x your_script.py

回答by Adem ?zta?

Add to top of the code,

添加到代码顶部,

#!/usr/bin/python

Then, run the following command on the terminal,

然后,在终端上运行以下命令,

chmod +x yourScriptFile

回答by NlightNFotis

I suggest that you add

我建议你添加

#!/usr/bin/env python

instead of #!/usr/bin/pythonat the top of the file. The reason for this is that the python installation may be in different folders in different distros or different computers. By using envyou make sure that the system finds python and delegates the script's execution to it.

而不是#!/usr/bin/python在文件的顶部。这样做的原因是python安装可能在不同发行版或不同计算机的不同文件夹中。通过使用,env您可以确保系统找到 python 并将脚本的执行委托给它。

As said before to make the script executable, something like:

如前所述,使脚本可执行,例如:

chmod u+x name_of_script.py

should do.

应该做。

回答by sparsh turkane

1.save your file name as hey.py with the below given hello world script

1.使用下面给出的hello world脚本将您的文件名保存为hey.py

#! /usr/bin/python
print('Hello, world!')

2.open the terminal in that directory

2.在那个目录下打开终端

$ python hey.py


or if you are using python3 then

或者如果你使用的是 python3 那么

$ python3 hey.py


回答by 100RaBH

If you have python 3 installed then add this line to the top of the file:

如果您安装了 python 3,则将此行添加到文件顶部:

 #!/usr/bin/env python3

You should also check the file have the right to be execute. chmod +x file.py

您还应该检查文件是否具有执行权限。chmod +x 文件.py

For more details, follow the official forum:

更多详情,请关注官方论坛:

https://askubuntu.com/questions/761365/how-to-run-a-python-program-directly

https://askubuntu.com/questions/761365/how-to-run-a-python-program-directly