Running a basic python script in Command Line


3 posts
by Megabeaker » Mon Jun 19, 2017 11:26 am
Hey all,

I've written a basic script to turn on an LED light attached to a breadboard. All seems to work fine when using the GUI in Raspbian however I would like to run it using command line. This is a basic exercise to help me with something else I'm doing.

the code is

Code: Select all
#!/usr/bin/python
from gpiozero import LED
led = LED(25)
led.on()


it's saved /home/pi/Desktop and is called led_on.py

I've tried entering the following

Code: Select all
python /home/pi/Desktop/led_on.py


and get a message saying
[3] Done led_on.py

but the LED does not light up. Like I say, when writing the code directly into python or running the module within it, everything works fine.

Thank
Posts: 3
Joined: Tue Jun 13, 2017 3:46 pm
by B.Goode » Mon Jun 19, 2017 11:35 am
The led probably does light up, but immediately goes out again when the script terminates, which happens so quickly you never notice.

(When you run it 'manually' via IDLE or interactively via the python interpreter the led will stay on until you close the IDE or the interpreter.)

Some options are to use time.sleep() [ need to import the time module ] or signal.pause() [ need to import the signal module ]

Since you are using the gpiozero module, see the first 2 examples here: https://gpiozero.readthedocs.io/en/stable/
Posts: 3717
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK
by Megabeaker » Mon Jun 19, 2017 11:53 am
Brilliant that works, thank you, so if I wanted the script to run the whole time my pi was on, what would be the best way of doing that?
Posts: 3
Joined: Tue Jun 13, 2017 3:46 pm