How to run Python script with one icon click?

15,845

Solution 1

Adding " #!/usr/bin/env python " at the top of the .py file works! Hmm, although don't appreciate the pop-up, but nevermind. :P

From 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

Solution 2

You should make the .py files executable and click on them. The .pyc files cannot be run directly.

Solution 3

install launcher software in ubuntu 12.04 step 1. paste this command in terminal without quotes

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

Step 2. now launch it by ..

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

Step : in command textbox write python path_of_your_pyc_file/filename.pyc

eg python /opt/test.pyc

and haha!! you have done.. congrats :)

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

Share:
15,845
A. K. Tolentino
Author by

A. K. Tolentino

Front-end: Angular Back-end: NodeJS or Django DevOps: GitLab CI/CD Infrastructure: Firebase, AWS Database: Firestore, Postgresql Experience: since May 2014 (6+ years)

Updated on June 12, 2022

Comments

  • A. K. Tolentino
    A. K. Tolentino over 1 year

    Sorry, for the vague question, don't know actually how to ask this nor the rightful terminologies for it.

    How to run a python script/bytecode/.pyc (any compiled python code) without going through the terminal. Basically on Nautilus: "on double click of the python script, it'll run" or "on select then [Enter], it'll run!". That's my goal at least.

    When i check the "Allow executing of file as a program" then press [enter] on the file. It gives me this message:

    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?

    Using Ubuntu 12.04, by the way and has to be python 2, one of the packages doesn't work on python 3. If there's a difference between how to do it on the two version, include it, if it's not to much t ask, thank you.

    I know it doesn't matter, but it's a script auto renaming & arranging my music files. Guide me accordingly, stupid idiot here. :)