Forums

scheduled task giving configparser.NoSectionError, when same script runs fine otherwise

I have a script that uses the ConfigParser module to grab settings from a config.ini file. The script runs fine on my local machine, and it runs fine on PythonAnywhere if I run it manually. It's only giving me errors when I run the script through the scheduled tasks interface.

I can get the error with this script:

import configparser

config = configparser.ConfigParser()
config.read("config.ini")

def main():
    interval = config.get("scriptsettings", "check_interval")

if __name__ == '__main__':
    main()

The scheduled task log looks like this:

Traceback (most recent call last):
  File "/usr/lib/python3.6/configparser.py", line 1135, in _unify_values
    sectiondict = self._sections[section]
KeyError: 'scriptsettings'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/unchow/project/schedule_test.py", line 11, in <module>
    main()
  File "/home/unchow/project/schedule_test.py", line 8, in main
    interval = int(config.get("scriptsettings", "check_interval"))
  File "/usr/lib/python3.6/configparser.py", line 778, in get
    d = self._unify_values(section, vars)
  File "/usr/lib/python3.6/configparser.py", line 1138, in _unify_values
    raise NoSectionError(section)
configparser.NoSectionError: No section: 'scriptsettings'

2018-08-21 16:30:59 -- Completed task, took 28.00 seconds, return code was 1.

If I run a script without the config.get line as a scheduled task, everything works fine. if I run this script from a Python Anywhere bash console, it also works fine. Im running python3.6, both in the bash console and the scheduled tasks.

The test script is also in the same directory as the config.ini file, and all of my other main scripts, so I know it's not a directory issue. From what I can tell, the script is finding the file, just not the relevant section within the file. I've also tested this with multiple different sections within the config file. Im also only using the standard library for this project, so I'm not using a virtualenv or anything like that.

What would cause configparser in particular to act differently when the script is run as a scheduled task? As far as I can tell, that's the only thing that's breaking for me right now.

For a scheduled task, the task is running from a different working directory. (eg: if it is running from /home/yourusername/, instead of /home/yourusername/path/to/your/file/)

So it is probably not able to find your config.ini.

Either use a full path (eg: /home/yourusername/path/to/config.ini), or perhaps play around with something like os.chdir(os.path.basename(__file__).

Gotcha! Yeah, that was absolutely the problem, thanks!

Just for the sake of completeness, replacing the config.get line from above with this line made it work nicely:

config.read("/home/unchow/project/config.ini")

I might suggest mentioning this somewhere in the scheduled tasks page, because I don't know that I would have ever figured that out on my own.

Thanks for the help!

Thanks for posting the solution! Re: adding help about this, did you check out any of the help pages on scheduled tasks? None of them currently mention the working directory stuff, but at least one of them definitely should, so it would be useful to know which ones you checked so that we know where to put it :-)

I was mostly working off of this "scheduled tasks" help page. If there were others, I don't think I found them. I think I was just using the search feature to find a page about how to set up scheduled tasks, so once I found one I assumed that that's all there was (aside from more specialized pages like the "long running tasks" page, which I also found but seems less directly relevant to this issue).

I'm digging your guy's support, btw.

Glad you like the support :-)

I've added a new "Make sure you take account of the working directory" section to that help page -- it would be good to know if that would have been enough to make the cause of the problem reasonably obvious to you .

That would have solved my problem before it arose, for sure.

Though I did notice a small typo:

...which would mean that it would work in when you run it like that from bash...

You should just delete the word "in" from that sentence, I think. Thanks again!

thanks! made the chgs :D