自动可执行的python文件,而无需从终端打开? [英] Auto executable python file without opening from terminal?

查看:185
本文介绍了自动可执行的python文件,而无需从终端打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这是在错误的网站上(也许是超级用户),但是我正在尝试使python.py文件可执行,因此我可以单击它,并且它会自动执行其操作,而无需在终端中指定要打开的文件默认提示下,我已经有了'chmod + x'的权限.

Sorry if this is on the wrong site ( maybe superuser ) but I'm trying to make my python.py file executable so I can click on it and it automatically does its thing, without me specifying it to open in the terminal by that default prompt, and I already have 'chmod +x' for its permissions.

说明:

  • 我想通过单击而不是通过终端来运行它(我的意思是当我说可以单击它并且它会自动执行操作"时)
  • 已经有一个shebang热线
  • 当我单击它时,它会提示您是否要在终端的文本文件中打开它-我可以始终将其默认设置为在终端中打开吗?或者这只是一个奇怪的请求?

推荐答案

首先,为要具有此行为的文件选择所需的文件扩展名. pyw可能是一个不错的选择.

First, pick a file extension you want for files you want to have this behavior. pyw is probably a good choice.

命名您的文件,然后在文件浏览器中将该文件类型与python关联.在GNOME中,您将打开其属性"窗口,转到打开方式"选项卡,然后输入python作为自定义命令.

Name your file that, and in your file browser associate that file type with python. In GNOME, you'd open its Properties window, go to the Open With tab, and enter python as a custom command.

现在这是重要的部分:您一直在问这个小对话框,您想对文件做些什么是因为它被标记为可执行文件.使用chmod -x除去可执行位.现在,当您双击它时,它将简单地与相关程序一起打开.

Now here's the important part: That little dialog you've been getting asking you what you'd like to do with the file is because it is marked as executable. Remove the executable bit with chmod -x. Now when you double click it, it will simply be opened with the associated program.

当然,如果要从命令行运行它,则由于它没有被标记为可执行文件,因此现在必须显式地使用python启动它. shebang行不再重要,但是无论如何,我还是将其保留,以防其他人将其标记为可执行文件并期望它能工作.

Of course, if you want to run it from the command line, you'll now have to start it with python explicitly since it isn't marked executable. The shebang line doesn't matter anymore, but I'd leave it in anyway in case someone else marks it executable and expects it to work.

这篇关于自动可执行的python文件,而无需从终端打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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