Linux Desktop 可执行 Python 脚本



我试图使一个简单的python脚本可执行,因此在谷歌上搜索该怎么做。这是我到目前为止得到的

。桌面

[Desktop Entry]
Version=1.0
Type=Application
Name=helloworld
Comment=
Exec=./test.py
Icon=
Path=/home/xxx/Desktop
Terminal=true
StartupNotify=false

蟒蛇文件

#!/usr/bin/env python
print('hello world')

在终端上,我做了 chmod +x test.py,现在可以通过 ./test.py 在终端中执行它

如果我双击桌面图标,我可以看到终端打开了很短的时间,但随后它关闭得非常快。

我做错了什么?

我希望桌面图标打开终端,然后显示我的 python 脚本。

谢谢

脚本完成后,终端窗口将关闭。你可以把

input()     # Python 3
raw_input() # Python 2

在脚本底部关闭 按回车键。

@Veedrac这不是

正确的方法。正确的方法是使用time.sleep(),它在Python 3.x和2.x中都有效。使用以下代码执行此操作。

import time # Should be the first statement.
# Some code is below. This code is useless. 
print()
def blah():
    print('bhahfdjfdk')
blah()
# When the program ends, use the code below to keep it running for some more time.
time.sleep(2) # In the parentheses you can replace 2 with the number of seconds you want to put the program on hold. This will help you and is the official Python way.

最新更新