2

I wish to python programs execute in a window from clicking an icon.

This is day one of teaching myself and type of coding and Ubuntu so over-explantion and extra detail is appreciated.

Code example I am using

print("Game Over")
input("\n\nPress the enter key to exit")

Runs OK in IDLE but when I click the .py file icon it an editor opens instead of program/window

Steps I have done so far

  • right clicked the .py file and set permissions to "allow executing file as program"
  • read as much as i could and it seems to be pointing me to information that is above my head.

Examples

#!/usr/local/bin/python
#!/usr/bin/env python
CHMOD X

None of which I understand or know where to use.

I am using Ubuntu 14.04 and Python 3

Thank you in advance.

Registered User
  • 9,631
  • 14
  • 53
  • 85

3 Answers3

9

Although your question might be on the edge for more than one reason (too broad, off topic, more than one subject per question, on the edge of a number of almost-duplicates), I'll answer the question(s).

The first question: about running a (text-only) script "from an icon", as you mention it:

  • Your script is text-only, and needs to be run in either Idle or a terminal window. That means that if you want to run it by double-clicking from an icon, you'd need to create a .desktop file, in which is defined to run the script inside a terminal window.
    These .desktop files are part of practically all GUI applications installed on Ubuntu by the way.

    A very basic example, fit for your script:

    [Desktop Entry]
    Name=Test
    Exec=/home/jacob/Bureaublad/test.py
    Terminal=true
    Type=Application
    

    The most interesting lines are:

    Exec=/home/jacob/Bureaublad/test.py
    

    in which the command to run your script is defined.
    Read more on how to create the command, the script being executable or not, using the shebang, language extension, see here.

    and the line:

    Terminal=true
    

    That says the script needs to be run in a terminal window.

    You can extend your .desktop file with a.o. an icon and a lot more options, depending on what you are using it for exactly, see here.

  • How to use the .desktop file

    • Paste the code above in an empty file, save it as test.desktop. Edit the command in the line Exec=/home/jacob/Bureaublad/test.py, according to the link I added to create commands to run a script.
    • If you use the file from your desktop, make it executable with the command:

      chmod +x /path/to/Test.desktop
      

    Alternatively, you can copy (move) the .desktop file to ~/.local/share/applications to make it available in Dash. Globally installed applications store their .desktop files in /usr/share/applications. In the last two directories, there is no need to make the .desktop file executable.

About the example lines you don't understand

  • The lines:

    #!/usr/local/bin/python
    #!/usr/bin/env python
    

    are shebangs; the first line of a script, telling the shell how to run it if the script is executable, and you run it without python before the path to the script. Since you use python3, the shebang in your scripts should normally be:

    #!/usr/bin/env python3
    

    More on this, and the relation between shebang and command in the link above.

  • CHMOD X (?) chmod +x is probably what you mean. As explained above, you can make a file executable with the command:

    chmod +x /path/to/file
    
Jacob Vlijm
  • 83,767
  • All around awesome. I appreciate you taking the time on this to answer so thoroughly. I will go back and apply this info. Thanks again! – strangeagent Mar 14 '15 at 19:58
  • @strangeagent You're welcome! One little thing: if this answer turns out to be what you are looking for, would you consider accepting it? (the big "V" on the left). Else the question will keep re-appearing as "unanswered". – Jacob Vlijm Mar 14 '15 at 20:02
0

As mentioned by others, chmod +x is necessary to make the program executable. For example for the program hello.py, you'd enter this in the terminal:

$ chmod +x hello.py

Now, don't know if it's true in former releases, but in Ubuntu 16.04 LTS it's necessary that the behavior of the icons be set to allow clicking on the icon to execute the program. You can do this in the following way:

Go to a file browser window, then move mouse cursor to the top of the screen and select edit > preferences > behavior, then select either 'Run executable text files when they are opened' or 'Ask each time'.

Also, to keep the terminal from closing at the end of the program (for example if you want to keep output of the program to the terminal on the screen), in python3.5 you need to put this line at the end of the program:

input()

Hope this helps.

-1

Write a .bat file, which runs under command prompt.In the file, write just one line like: C:/Python/Python38/python.exe c:/MyProject/mainFile.py

Where, C:/Python/Python38/python.exe is Python 3.8, and c:/MyProject/mainFile.py is the python script file to be executed.

Create a shortcut of the .bat file and place it on Windows screen as an icon. That is it.

BobXu
  • 1