skiwithpete / alarmpi

This is the latest version of the alarm clock for Raspberry Pi project.
116 stars 49 forks source link

Not able to run script outside of install directory #3

Closed DerfOh closed 8 years ago

DerfOh commented 9 years ago

Not sure where the issue is but when I try to run the script outside of the alarmpi directory. For example if I run it from the pi user directory I get the following output. pi@raspberrypi ~ $ python alarmpi/sound_the_alarm.py Traceback (most recent call last): File "alarmpi/sound_the_alarm.py", line 31, in head = Config.get('main','head')+" " File "/usr/lib/python2.7/ConfigParser.py", line 607, in get raise NoSectionError(section) ConfigParser.NoSectionError: No section: 'main' Looks like it's having problems with the config file but no errors are thrown when I run it from within the alarmpi directory.

As a result I'm also unable to get the cron job to properly run. Any ideas?

ghost commented 8 years ago

it needs code like this instead

Config.read(path.dirname(path.realpath(__file__)) + '/alarm.config')

BUT, this path should really be a variable so you can pass the path to a configuration file outside of the git repository

virajparikh4 commented 8 years ago

can you elaborate? how to make this work outside of install directory?

cpenning commented 8 years ago

Here's how I fixed this:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
os.chdir(os.path.dirname(sys.argv[0])) # When called from cron, we can find our code

import ConfigParser
...
DerfOh commented 8 years ago

Thanks! I put this project up on a shelf a long time ago, I will dust it off and try your fix though! For now I will close this issue though.