How to run a Python script with one click of the icon?

Sorry for the vague question, I don’t really know how to ask about this or the legal terminology for him.

How to run python script / bytecode / .pyc (any compiled Python code) without going through the terminal. Mostly on Nautilus: "when you double-click the python script it will run" or "when you select [Enter], it will work!". That is at least my goal.

When I check "Allow the file to run as a program," then press [enter] in the file. This gives me the following message:

Failed to display "/ home / ghelo / Music / arrange .pyc". No application is installed for Python bytecode files. Do you want to find an application to open this file?

Using Ubuntu 12.04, by the way, it should be python 2, one of the packages does not work on python 3. If there is a difference between how to do this in the two versions, turn it on if it doesn’t ask much, thanks.

I know this doesn't matter, but this is a script to automatically rename and organize my music files. Guide me stupid idiot. :)

+6
source share
3 answers

Adding "#! / Usr / bin / env python" to the top of the .py file works! Hmm, although I don't appreciate the pop-up, it doesn't matter .: P

From PHPUG:

You are not calling the pyc file. This is the .py file called. Python is an interpreted language.

The simplest way to make python exectuable (explained):

1) Add #! / Usr / bin / env python to the top of the python executable (e.g. main.py) (it uses python by default - for example, if arch is used, then py3 instead of py2. Can explicitly tell it to run python2 / python3, replacing python with its version: ex. python2.7)

2) Write the code. If the script is called directly, the __name__ variable becomes equal to the string '__main__', thus the idiom: if __name__ == '__main__' :. You can add all the logic related to your script directly called in this if-block. This allows you to import your executable file.

3) Make it executable "chmod + x main.py"

4) Call script: ./ main.py args args

+1
source

You must make the executable .py executable and click on it. Cannot start .pyc files.

+5
source

install the software to run in ubuntu 12.04 step 1. Paste this command into the terminal without quotes

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

Step 2. Now run it ..

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

Step: in the text field of the write python command path_of_your_pyc_file / filename.pyc

For example, python / opt / test.pyc

and ha ha !! you did .. congratulations :)

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

+1
source

Source: https://habr.com/ru/post/922294/


All Articles