[英]how to make python script executable when click on the file
I am trying to make my python script executable without going through the terminal typing like 我试图使我的python脚本可执行而无需通过类似的终端输入
python test.py python test.py
I want to make it able to run when i click on the file. 我想让它能够在我点击文件时运行。
How i going to do this in my fedora machine. 我如何在我的fedora机器上做这个。
Add #!/bin/python
as the very first line of your file. 添加
#!/bin/python
作为文件的第一行。 Or, if you don't know where your python executable is, type which python
in a terminal; 或者,如果您不知道python可执行文件的位置,请键入终端中的
which python
; then copy the result of that and put it after the #!
然后复制那个结果并把它放在
#!
之后#!
. 。
Change the permissions of the file so that its executable chmod u+x test.py
更改文件的权限,使其可执行文件
chmod u+x test.py
i try but it still open back as gedit
我尝试但它仍然打开gedit
python
in the command to be added. python
即可。 #!/usr/bin/env python
at the very beginning of file. #!/usr/bin/env python
。 chmod u+x filename.py
chmod u+x filename.py
.py
to .sh
, so your linux distro's UI will recognize it as shell script and try to execute. .py
更改为.sh
,以便您的Linux发行版的UI将其识别为shell脚本并尝试执行。 It's Nautilus's fault. 这是鹦鹉螺的错。 Open Nautilus (the file manager), go to Menu > Preferences.
打开Nautilus(文件管理器),进入菜单>首选项。 Select the "Behaviour" section.
选择“行为”部分。 On the field titled "Executable text files", select the option "Execute executable text files when opened".
在标题为“可执行文本文件”的字段上,选择“打开时执行可执行文本文件”选项。
添加#!/usr/bin/python
作为文件的第一行,并将权限设置为可执行chmod 755 yourfile
。
如果你没有任何特定的版本要求,那么使用第一行作为#!/usr/bin/env python
将更有效并给予执行权限chmod u+x test.py
I know it can be too late, but i did have the same idea. 我知道这可能为时已晚,但我确实有同样的想法。 (run python scripts in fedora) and found some trouble.
(在fedora中运行python脚本)并发现一些麻烦。 My suggestion to you is to make a launcher with a
.sh
file, like this: 我的建议是使用
.sh
文件创建一个启动器,如下所示:
#!/bin/sh
gnome-terminal -x python yourscript.py
Give the permition to execute with chmod +x file.sh
, them click and will run. 使用
chmod +x file.sh
赋予permition执行chmod +x file.sh
,然后单击并运行。
[^_~]
I use raspibian os (Linux) 我用raspibian os(Linux)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.