如何在单击一个图标的情况下运行Python脚本?

2024-06-06 04:59:02 发布

您现在位置:Python中文网/ 问答频道 /正文

对不起,对于这个含糊不清的问题,我不知道该怎么问,也不知道该怎么说。

如何在不经过终端的情况下运行python脚本/bytecode/.pyc(任何已编译的python代码)。基本上在Nautilus上:“双击python脚本,它将运行”或“在select上,然后[输入],它将运行!”。至少这是我的目标。

当我选中“允许将文件作为程序执行”时,请按文件上的[输入]。它给了我这个信息:

Could not display "/home/ghelo/Music/arrange.pyc". There is no application installed for Python bytecode files. Do you want to search for an application to open this file?

顺便说一下,使用Ubuntu 12.04必须是python 2,其中一个包不能在python 3上工作。如果在两个版本上有什么不同,包括它,如果不是问得太多,谢谢。

我知道这不重要,但这是一个脚本自动重命名和安排我的音乐文件。相应地引导我,愚蠢的白痴。:)


Tags: 文件to代码脚本信息终端目标for
3条回答

您应该使.py文件可执行并单击它们。无法直接运行.pyc文件。

在ubuntu 12.04中安装启动程序软件 第一步。将此命令粘贴到不带引号的终端

"sudo apt-get install --no-install-recommends gnome-panel"

第二步。现在启动它。。

gnome-desktop-item-edit --create-new ~/Desktop

步骤:在命令文本框中写入 python path_of_your_pyc_file/filename.pyc

eg python /opt/test.pyc

哈哈!!你已经做了。。恭喜:)

please check link how to install launcher here https://askubuntu.com/questions/64222/how-can-i-create-launchers-on-my-desktop

添加“#”!/usr/bin/env python“在.py文件的顶部工作!嗯,虽然不喜欢弹出窗口,但别客气。:P页

来自PHPUG:

You do not invoke the pyc file. It's the .py file that's invoked. Python is an interpreted language.

A simpler way to make a python exectuable (explained):

1) Add #!/usr/bin/env python at the top of your python executable file (eg. main.py) (it uses the default python - eg. if using arch, that's py3 instead of py2. You can explicitly tell it to run python2/python3 by replacing python with it's version: ex. python2.7)

2) Write the code. If the script is directly invoked, __name__ variable becomes equal to the string '__main__' thus the idiom: if __name__ == '__main__':. You can add all the logic that relates to your script being directly invoked in this if-block. This keeps your executable importable.

3) Make it executable 'chmod +x main.py'

4) Call the script: ./main.py args args

相关问题 更多 >