How to run Python script with one icon click?
Asked Answered
L

3

6

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

Leicestershire answered 7/8, 2012 at 14:55 Comment(2)
Have you tried running the *.py files?Bushwa
yup, but to no avail. :(Leicestershire
L
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

Leicestershire answered 18/8, 2012 at 3:14 Comment(0)
P
4

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

Phantasm answered 7/8, 2012 at 14:57 Comment(4)
It gives me the message: Do you want to run "__arrange__.py", or display its contents? i click on 'run' (also run on terminal) doesn't do anything. :(Leicestershire
Is __arrange__.py an entry module? Doesn't sound like something that's to be executed. What do you get when you try to run it from the command line?Phantasm
It work's good! :D (although not yet to it's purpose) it creates the file named: 'test.txt', as expected for the test script. But not when "pressing enter". Btw, when i do choose 'run on terminal' it pops up the terminal for a sec. but disappears. Hope that helps.Leicestershire
PS. Not familiar on the term "entry module". Sorry, :)Leicestershire
L
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

Leicestershire answered 18/8, 2012 at 3:14 Comment(0)
I
1

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

Ilbert answered 15/3, 2014 at 18:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.