Python Forum
raspberry pi in a python script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
raspberry pi in a python script
#1
Quote:Hey everyone
so I have this simple function for my raspberry pi
and I made a python file it does work

from gpiozero import LED
from gpiozero.pins.pigpio import PiGPIOFactory

# this IEP address is assigned to raspberry pi
factory = PiGPIOFactory(host='pi's IP address')
Switch_1 = LED(18, pin_factory=factory)
Switch_2 = LED(23, pin_factory=factory)
Switch_3 = LED(24, pin_factory=factory)


Switch_1.on()
Switch_2.on()
Switch_3.on()
But the problem I'm running into
I tried to merge it into another python file
and put it under a function which I called
def raspberry_pi():
but for whatever reason it doesn't work I'm having a hard time understanding why

Under that function I can call the file and it will work
I just can't put everything under the function I don't understand what's going on

Has anyone ran into this problem
Reply
#2
You should post the code for the other python file. The one that makes this script into a function. The one that has the problem you are asking about.
Reply
#3
Agree. Did you indent the existing code in your function definition?
Reply
#4
(Jun-05-2024, 10:02 PM)jefsummers Wrote: Agree. Did you indent the existing code in your function definition?
the code does work independently by itself
so there's no reason it should not work the same when I place it in a function
but it doesn't
if you know the reason please explain it to me
Reply
#5
The reason it doesn't work is because you introduced errors changing the code from a script to a function. This is not only possible, but highly likely. Indenting your script and putting def func(): in front of it usually doesn't result in runnable code. Please post the code that doesn't run, including code that calls the new function. There are too many ways to mess this up for me to guess which one(s) you chose.
Reply
#6
Again agree, please post the code that does not work. Posting code that does work is not something we can help with!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Building specific Python version on Raspberry PI 5 (Raspbian) andrewk 2 721 Feb-03-2025, 11:41 AM
Last Post: iterate
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 8,190 Jun-29-2023, 11:57 AM
Last Post: gologica
  Video recording with Raspberry Pi - What´s wrong with my python code? Montezuma1502 3 2,411 Feb-24-2023, 06:14 PM
Last Post: deanhystad
  Coding for Python and Raspberry pi beast 3 64,073 Sep-21-2021, 09:56 PM
Last Post: beast
  python 3 raspberry pi 4 dual control motor programming problem yome 0 2,646 Mar-21-2021, 05:17 PM
Last Post: yome
  Embedded python fails to compile on Raspberry Pi tryfon 2 4,666 Dec-22-2020, 02:06 PM
Last Post: tryfon
  Uploading files to NAS from Raspberry PI using python zazas321 0 3,259 Sep-07-2020, 09:02 AM
Last Post: zazas321
  Port my python program to Raspberry pi seamlessly Hassibayub 1 2,630 Jun-29-2020, 12:58 PM
Last Post: snippsat
  How to kill a bash script running as root from a python script? jc_lafleur 4 8,079 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 3,007 May-28-2020, 05:27 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020