Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Python script with one icon click?

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. :)

like image 856
A. K. Tolentino Avatar asked Aug 07 '12 14:08

A. K. Tolentino


2 Answers

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

like image 125
Noufal Ibrahim Avatar answered Oct 10 '22 01:10

Noufal Ibrahim


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

like image 39
A. K. Tolentino Avatar answered Oct 10 '22 02:10

A. K. Tolentino