kobbe
Posts: 5
Joined: Tue Dec 25, 2012 9:24 pm

phyton executable?

Wed Dec 26, 2012 12:59 am

When i am trying to make an executeble file from my phyton code it does not work. I have added #! /usr/bin/env python in my file and clicked the box on properties that makes an executeable file. When i run it (and choose run i terminal) nothing happens!

Some input on this?

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2848
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: phyton executable?

Wed Dec 26, 2012 9:52 am

you can run Python files in a couple of ways

Code: Select all

python filename.py
it does not need to be executable

If you want it to be executable, apart from the shebang, you also need to

Code: Select all

chmod +x filename.py
if the file is outside your home directory it would need to be

Code: Select all

sudo chmod +x filename.py
this changes the file permissions so it is executable.
Alex Eames RasPi.TV, RasP.iO

kobbe
Posts: 5
Joined: Tue Dec 25, 2012 9:24 pm

Re: phyton executable?

Wed Dec 26, 2012 1:27 pm

It still wont work, absolutely nothing happens! When i run /usr/bin/env python in the terminal it starts python as expected. If I start with python myfile.py it works!

trouch
Posts: 310
Joined: Fri Aug 03, 2012 7:24 pm
Location: France
Contact: Website

Re: phyton executable?

Wed Dec 26, 2012 3:10 pm

in yourscript.py, the first line should be :

Code: Select all

#!/usr/bin/python
then set permissions :

Code: Select all

chmod +x yourscript.py
then execute it :

Code: Select all

./yourscript.py
the heading dot and slash are required as the shell does not look up in the current directory when calling programs.

if you want to use it from anywhere, you need to add its folder to $PATH or copy the script somewhere in $PATH (/usr/bin for instance)

WebIOPi - Raspberry Pi REST Framework to control your Pi from the web
http://store.raspberrypi.com/projects/webiopi
http://code.google.com/p/webiopi/
http://trouch.com

kobbe
Posts: 5
Joined: Tue Dec 25, 2012 9:24 pm

Re: phyton executable?

Wed Dec 26, 2012 3:57 pm

Yes, that totally works but it is as bad as writing "python name.py" to start it. I want to start the script from the desktop too, from the filesystem. There is gives me 4 options when clicked: Execute, Execute in terminal, open or cancel. When i press execute or execute in terminal nothing happens! The script should start a new terminal when i press "execute in terminal" and then execute itself?

I.e I want to start it from "desktop mode". That does not work

trouch
Posts: 310
Joined: Fri Aug 03, 2012 7:24 pm
Location: France
Contact: Website

Re: phyton executable?

Wed Dec 26, 2012 4:05 pm

if you want to start from the desktop, you have to provide the full path if files are not in $PATH :

Code: Select all

python /home/pi/myapp/myscript.py
or

Code: Select all

/home/pi/myapp/myscript.py
you have to check "execute in terminal"

WebIOPi - Raspberry Pi REST Framework to control your Pi from the web
http://store.raspberrypi.com/projects/webiopi
http://code.google.com/p/webiopi/
http://trouch.com

kobbe
Posts: 5
Joined: Tue Dec 25, 2012 9:24 pm

Re: phyton executable?

Wed Dec 26, 2012 4:29 pm

I want to use "desktop mode" NOT use the terminal! I want to click on it with my mouse and the script will run.

When the file is clicked with the mouse on it gives me 4 options: Execute, Execute in terminal, open or cancel. When i press execute or execute in terminal nothing happens! The script should start a new terminal when i press "execute in terminal" and then execute itself?

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2848
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: phyton executable?

Wed Dec 26, 2012 4:41 pm

Just noticed you've got a space in your shebang line after the !

#! /usr/bin/env python

I wonder if that's causing you problems?

I don't use the desktop much so can't help you with that - sorry.
Alex Eames RasPi.TV, RasP.iO

kobbe
Posts: 5
Joined: Tue Dec 25, 2012 9:24 pm

Re: phyton executable?

Wed Dec 26, 2012 5:26 pm

No, the space does not make any diffrence. I am using the raspbian "wheezy".

trouch
Posts: 310
Joined: Fri Aug 03, 2012 7:24 pm
Location: France
Contact: Website

Re: phyton executable?

Wed Dec 26, 2012 5:57 pm

Just tried to open an executable bash script from the file explorer, and you are right, nothing happens...
So it's not a python issue, but a general executable script issue with lxde

WebIOPi - Raspberry Pi REST Framework to control your Pi from the web
http://store.raspberrypi.com/projects/webiopi
http://code.google.com/p/webiopi/
http://trouch.com

User avatar
jojopi
Posts: 3079
Joined: Tue Oct 11, 2011 8:38 pm

Re: phyton executable?

Wed Dec 26, 2012 6:45 pm

This is apparently a known issue with the default settings in the file manager, pcmanfm, that affects many Debian-derived systems.

Click Edit→Preferences→Advanced and change the terminal emulator command from "x-terminal-emulator %s" to "x-terminal-emulator -e %s".

Presumably some terminal emulators are forgiving of the missing -e, but lxterminal is not.

trouch
Posts: 310
Joined: Fri Aug 03, 2012 7:24 pm
Location: France
Contact: Website

Re: phyton executable?

Wed Dec 26, 2012 6:59 pm

jojopi wrote:This is apparently a known issue with the default settings in the file manager, pcmanfm, that affects many Debian-derived systems.

Click Edit→Preferences→Advanced and change the terminal emulator command from "x-terminal-emulator %s" to "x-terminal-emulator -e %s".

Presumably some terminal emulators are forgiving of the missing -e, but lxterminal is not.
Works for me, thanks !

WebIOPi - Raspberry Pi REST Framework to control your Pi from the web
http://store.raspberrypi.com/projects/webiopi
http://code.google.com/p/webiopi/
http://trouch.com

User avatar
marciokoko
Posts: 291
Joined: Sat Aug 27, 2016 4:33 pm

Re: phyton executable?

Sun May 13, 2018 11:58 pm

i have the following python script on a rpi3 and I also have the same issue, it wont run when i double click on the desktop. I dont see that terminal emulator advanced settings option and ive already dont the chmod +x gui.py:

Code: Select all

from Tkinter import *
import tkFont
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.OUT)
GPIO.output(40, GPIO.LOW)

win = Tk()

myFont = tkFont.Font(family = 'Helvetica', size = 36, weight = 'bold')

def ledON():
        print("LED button pressed")
        if GPIO.input(40) :
                GPIO.output(40,GPIO.LOW)
                ledButton["text"] = "LED ON"
        else:
                GPIO.output(40,GPIO.HIGH)
                ledButton["text"] = "LED OFF"

def exitProgram():
        print("Exit Button pressed")
        GPIO.cleanup()
        win.quit()


win.title("First GUI")
win.geometry('800x480')

exitButton  = Button(win, text = "Exit", font = myFont, command = exitProgram, heig$
exitButton.pack(side = BOTTOM)

ledButton = Button(win, text = "LED ON", font = myFont, command = ledON, height = 2$
ledButton.pack()

mainloop()

User avatar
B.Goode
Posts: 8256
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: phyton executable?

Mon May 14, 2018 5:42 am

Please re-read the replies given early in this thread. You do not have an initial line in the file identifying it as to be run by the python interpreter.

User avatar
marciokoko
Posts: 291
Joined: Sat Aug 27, 2016 4:33 pm

Re: phyton executable?

Mon May 14, 2018 3:24 pm

You mean this line:

#! /usr/bin/env python

When I add it, it pops up a window whether i want it to execute, execute in terminal or something else. When i click either one, i get an error saying it cannot find or the directory does not existe "/home/pi/ something else". ill have to try that again to see exactly what it is. Does that have something to do with the $PATH directory?

btcbilly
Posts: 1
Joined: Mon Nov 19, 2018 3:16 am

Re: phyton executable?

Mon Nov 19, 2018 3:22 am

It appears for some reason if you use chmod +x file.py that you still cannot run the file by a button click (or at least in my situation)

however you can open ruby scripts that way. so if you create a ruby script that opens up your python script you can then open the file with a button click.

ruby script can look like this

system("sudo python3 yourpythonscript.py")

Return to “Python”