mattg31
Posts: 79
Joined: Fri Jan 05, 2018 9:55 pm

Imports causing python script to hang at startup

Sun May 05, 2019 11:10 am

I have a python script running on a raspberry pi that I want to run automatically at startup. I had this working until I added a temperature sensor, and now the program wont run automatically at startup, but will run fine with no errors if I run it manually.

Is there something I don't know about certain imports at startup?

What works:

I modify `rc.local` by adding `python /home/pi/myscript.py &` near the end of the file.

where myscript.py =

Code: Select all

    #!/usr/bin/python3
  
    import RPi.GPIO as GPIO #for GPIO naming convention
    import os #for power circuit
    import serial #for reading serial port on USB
    import time
    import threading 

    def power_button():

	    GPIO.setmode(GPIO.BCM)#set to GPIO Values
	    GPIO.setwarnings(False)
	    GPIO.setup(4,GPIO.OUT)#relay coil
	    GPIO.setup(22,GPIO.OUT)#power LED
	    GPIO.setup(27,GPIO.IN)#power button sense
	    GPIO.output(4,GPIO.HIGH)#turn on coil
	    GPIO.output(22,GPIO.HIGH)#turn on power LED
	    powerButton = GPIO.input(27)

	    while powerButton ==1: #check for change, then shutdown
		    powerButton = GPIO.input(27)
		
	    #change occured, as program left the while loop, proceed below
	    GPIO.output(22,GPIO.LOW)#turn off power LED
	    os.system('sudo shutdown -h now')

	
    #power button thread
    thread1 = threading.Thread(target=power_button) 
    thread1.start() 
Like mentioned above, this code works and runs properly at startup automatically.

What doesn't work:
When I add my temperature sensor imports, it will no longer start automatically, but will run fine if I run manually after the pi boots up.

myscripy.py with more imports added:

Code: Select all

    #!/usr/bin/python3

    import RPi.GPIO as GPIO #for GPIO naming convention
    import os #for power circuit
    import serial #for reading serial port on USB
    import time
    import threading 
    import adafruit_sht31d #sht temphumid sensor
    import board #sht temphumid sensor
    import busio #sht temphumid sensor
    import adafruit_sht31d #sht temphumid sensor
    import requests #for posting



    # Create library object using our Bus I2C port for SHT31 temp humid sensor
    i2c = busio.I2C(board.SCL, board.SDA)
    sensor = adafruit_sht31d.SHT31D(i2c)


    def power_button():

	    GPIO.setmode(GPIO.BCM)#set to GPIO Values
	    GPIO.setwarnings(False)
	    GPIO.setup(4,GPIO.OUT)#relay coil
	    GPIO.setup(22,GPIO.OUT)#power LED
	    GPIO.setup(27,GPIO.IN)#power button sense
	    GPIO.output(4,GPIO.HIGH)#turn on coil
	    GPIO.output(22,GPIO.HIGH)#turn on power LED
	    powerButton = GPIO.input(27)

	    while powerButton ==1: #check for change, then shutdown
		    powerButton = GPIO.input(27)
		
	    #change occured, as program left the while loop, proceed below
	    GPIO.output(22,GPIO.LOW)#turn off power LED
	    os.system('sudo shutdown -h now')

	
	
    #power button thread
    thread1 = threading.Thread(target=power_button) 
    thread1.start() 

Any ideas?

User avatar
bensimmo
Posts: 4623
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Imports causing python script to hang at startup

Sun May 05, 2019 1:09 pm

You probably need to log the errors how are you starting it?
It could just be a permissions problem (e.g. needs to be run as pi user)
Or it may need a delay so everything is up and ready.

(As a note, it looks like you are importing the same thing twice, sht31t ?)

User avatar
bensimmo
Posts: 4623
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Imports causing python script to hang at startup

Sun May 05, 2019 1:11 pm

I modify `rc.local` by adding `python /home/pi/myscript.py &` near the end of the file.
I must read better
Try changing to python3

It's often better to do (I've found anyway)

Code: Select all

cd /home/pi
su pi -c "python3 my script.py > thelogfile.log 2>&1"
That way you can see what is going on (any print statements and any error or crashes in the order they appear, normally)
Last edited by bensimmo on Sun May 05, 2019 1:16 pm, edited 1 time in total.

User avatar
scruss
Posts: 3212
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Imports causing python script to hang at startup

Sun May 05, 2019 1:16 pm

One of the libraries you're importing is expecting a network connection? The network might not be up by the time rc.local is run.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

mattg31
Posts: 79
Joined: Fri Jan 05, 2018 9:55 pm

Re: Imports causing python script to hang at startup

Mon May 06, 2019 9:57 am

Thanks for the feedback guys. I tried quite a few things to make this work, ultimately it was making it executable, then running it as pi user from rc.local (/bin/su -c "/home/pi/myscript.py" - pi &), then finally adding in a 10 second delay to the beginning of the program to let everything startup before all the imports occur.
Thanks again!

Return to “Python”