Ubuntu – Make a Python program executable from its icon

executableiconspython

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.

Best Answer

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
    
Related Question