0

I tried to make a simple python script executable, and therefore googled what to do. Here's what I got so far

.desktop

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

python file

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

On the terminal I did chmod +x test.py and now it is possible to execute it in the terminal via ./test.py

If I double click on the desktop icon I can see the terminal open for a really short time but then it closed really quick.

What am I doing wrong?

I expected the desktop icon to open the terminal and then shows my python script.

thank you

2 Answers 2

3

The terminal window will close when the script is finished. You can put

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

at the bottom of the script to close on enter press.

Sign up to request clarification or add additional context in comments.

4 Comments

need 12 more minutes to acceppt this ;)
is this the common way to do so?
I think so. Normally people don't run instantaneous scripts from a shortcut, though. Programs like Ranger already stay alive for long periods of time and other programs that don't are almost never run like this. // Some terminals support waiting on exit, but then you're locking yourself into a few terminals. This way you can use all of 'em.
the terminal is blank
0

@Veedrac that is not the correct way. The correct way is to use time.sleep() which works both in Python 3.x and 2.x. Use the following code to do that.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.