Joker56
Posts: 4
Joined: Fri Oct 13, 2017 3:25 pm

python astral raspberry pi

Fri Oct 13, 2017 9:39 pm

Hello all,

I made a python script who is calculating the sunrise and sunset for an certain date.
When i run that script in the terminal (putty) it works fine.
But i want to run that scrip at startup on my raspberry. Thats when it goes wrong.

I get this error:

Code: Select all

Traceback (most recent call last):
  File "/home/pi/calculate_light.py", line 6, in <module>
    from astral import Astral
ImportError: No module named astral
Does anybody knows why my script "calculate_light.py" runs fine in the terminal but not when booting my pi?

This is the code I'm talking about

Code: Select all

import datetime
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
#GPIO.setwarnings(False)
from astral import Astral
import subprocess
city_name = 'Brussels'
a = Astral()
a.solar_depression = 'civil'
city = a[city_name]
timezone = city.timezone
sun = city.sun(date=datetime.date.today(), local=True)
tzinfo = sun['dawn'].tzinfo
print('Dawn:    %s' % str(sun['dawn']))
print('Sunrise: %s' % str(sun['sunrise']))
print('Noon:    %s' % str(sun['noon']))
print('Sunset:  %s' % str(sun['sunset']))
print('Dusk:    %s' % str(sun['dusk']))
Thanks in advance

asavah
Posts: 296
Joined: Thu Aug 14, 2014 12:49 am

Re: python astral raspberry pi

Sat Oct 14, 2017 12:39 am

How exactly you are starting your script?

You get the error because because python doesn't know where to find your astral module because it not in python's sys.path

Two ways to fix this:
1) In your start script first

Code: Select all

cd /home/pi/
or if it's systemd service add

Code: Select all

WorkingDirectory=/home/pi
2) Add /home/pi to sys.path in the script itself before importing astral

Code: Select all

import sys
sys.path.insert(0, "/home/pi")
Here I assume that astral module is located in "/home/pi"

Joker56
Posts: 4
Joined: Fri Oct 13, 2017 3:25 pm

Re: python astral raspberry pi

Sat Oct 14, 2017 7:11 am

It still doesn't work.

this is the code from my launcher.sh script.

Code: Select all

cd /
cd /home/pi
sudo python calculate_light.py
cd /
added /home/pi to sys.path.

Code: Select all

import datetime
import time
import RPi.GPIO as GPIO
import sys
sys.path.insert(0, "/home/pi")
GPIO.setmode(GPIO.BOARD)
#GPIO.setwarnings(False)
from astral import Astral
import subprocess
city_name = 'Brussels'
a = Astral()
a.solar_depression = 'civil'
city = a[city_name]
timezone = city.timezone
sun = city.sun(date=datetime.date.today(), local=True)
tzinfo = sun['dawn'].tzinfo
print('Dawn:    %s' % str(sun['dawn']))
print('Sunrise: %s' % str(sun['sunrise']))
print('Noon:    %s' % str(sun['noon']))
print('Sunset:  %s' % str(sun['sunset']))
print('Dusk:    %s' % str(sun['dusk']))
now = datetime.datetime.now(tzinfo)
mydate =  datetime.datetime(2017, 10, 12, 5, 5, 5, 78915, tzinfo)
Maybe the astral module isn't located in the /home/pi folder?
I installed the astral module like this .

Code: Select all

pip install astral

DirkS
Posts: 7639
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: python astral raspberry pi

Sat Oct 14, 2017 9:41 am

Joker56 wrote:
Sat Oct 14, 2017 7:11 am

Code: Select all

pip install astral
You need admin permissions to install modules with pip, so you may need to use 'sudo pip'
BTW: I don't think you need sudo to run your script.

Joker56
Posts: 4
Joined: Fri Oct 13, 2017 3:25 pm

Re: python astral raspberry pi

Sat Oct 14, 2017 10:17 am

The problem is that my script runs fine in the terminal but won't run automatically when booting my rpi.
Thi is the error description:

Code: Select all

Traceback (most recent call last):
  File "/home/pi/calculate_light.py", line 6, in <module>
    from astral import Astral
ImportError: No module named astral

asavah
Posts: 296
Joined: Thu Aug 14, 2014 12:49 am

Re: python astral raspberry pi

Sat Oct 14, 2017 2:06 pm

asavah wrote:
Sat Oct 14, 2017 12:39 am
Here I assume that astral module is located in "/home/pi"
My assumption was wrong, because you did not provide enough information to get help.

Want help ? Provide info. We don't have psychic powers here.

You still didn't answer the question "How exactly you are starting your script?"
contents of your launcher.sh is not enough, how is it hooked? where? is it in rc.local? or is it a systemd service? or wahtever?

If you installed astral as user it will be available only to your user, not systemwide.

To reinstall it systemwide do:

Code: Select all

pip uninstall astral
sudo pip install astral

Joker56
Posts: 4
Joined: Fri Oct 13, 2017 3:25 pm

Re: python astral raspberry pi

Sat Oct 14, 2017 7:14 pm

Yes this solved this issue.
My script runs fine when my pi boots.
Thanks!

Return to “Python”

Who is online

Users browsing this forum: No registered users and 20 guests