简体   繁体   English

如何在单击文件时使python脚本可执行

[英]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机器上做这个。

  1. 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 #! 然后复制那个结果并把它放在#!之后#! .

  2. 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

  1. Right click on the file in your gnome file browser or desktop. 右键单击gnome文件浏览器或桌面中的文件。
  2. Select Properties 选择属性
  3. Go to Open with and choose Python. 转到打开方式并选择Python。 If you don't see python in the list, add the command. 如果在列表中没有看到python,请添加命令。 Just type python in the command to be added. 只需在要添加的命令中键入python即可。
  1. Add #!/usr/bin/env python at the very beginning of file. 在文件的最开头添加#!/usr/bin/env python
  2. Make chmod u+x filename.py 使chmod u+x filename.py
  3. Change your extension from .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)

  1. Add #!/usr/bin/python as the first line of the file.py 添加#!/ usr / bin / python作为file.py的第一行
  2. right click file >> open with >> chose customize >> custom command line >> type python3 右击文件>>打开>>选择自定义>>自定义命令行>>键入python3
  3. execute file with double click is working 双击执行文件正在运行

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

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