Forums

Where do scheduled tasks run from?

I've specified the path in the Your Scheduled Tasks field as absolute:

/home/username/Project Folder/subfolder1/subfolder2/script.py

In the script I want to import some stuff. Everywhere else in the project I can just do

from subfolder1.subfolder2.script import *

But I get the warning 'unable to detect defined names' and when the script runs, it fails cause it can't find the module.

Do you mean you have other .py files that need to import from script.py, in which case where are they?

Or do you want script.py to import from other .py files, in which case where are they?

script.py needs to import from other files, which are two subdirectories down from where the script is. Both subdirectories are python modules, and I have no problem importing from them in other (Django) scripts.

I've tried the obvious - os.chdir() to the right directory at the beginning of script.py - but it still doesn't see it. I'm afraid I'm feeling rather thickheaded at the moment.

So isn't your script at:

/home/username/Project Folder/script.py

?

Yes.

Ah - haha

I'm a moron

Got to sort out my sleep patterns. Thanks for your help.

dw - happens to ALL of us...

;----) Jim

Normally they'll run in your home directory. I think the best thing to do is to explicitly change the directory to what you want right at the start of your script, before any other imports, like this:

import os
os.chdir("/home/username/Project Folder/")

HTH

Right, so, if the script then calls other scripts, which in turn call others, all those relative paths are looked up from where the first script is running.

All my scripts (including the scheduled one) have their import statements done relative from the ProjectFolder directory. That's why I reflexively used

from subfolder1.subfolder2.script_lib import *

in the scheduled script. Changing it to just

from script_lib import *

sorted that out but obviously couldn't fix all the imports in the scripts that script_lib then went on to call.

I'd really like to just change directory to ProjectFolder and run the scheduled script from there. But os.chdir() doesn't seem to affect where Python looks to find the import scripts.

EDIT: posted before seeing giles' answer. But that's what I originally tried.

Whoops, got distracted before posting my reply and realised that you guys had a complete conversation and fixed the problem in the meantime :-)

OK, our posts are crossing in the, um, post.

If that's not working (and I'm surprised it's not) then perhaps the best solution is to set your python path explicitly:

import sys
sys.path.append("/home/username/Project Folder/")

{crosspost}

If that still doesn't help I can take a look at your files and see if there's anything non-obvious there. Just send me a message using the "Send feedback" link saying where they are in your private file storage.

Yep, that fixed it, I'm now onto error #2.

Okay, so now I'm trying to include some Django files, and to sort out path problems I've just copy/pasted everything from the wsgi.py file into the beginning of my scheduled script.

This works but doesn't feel particularly clean - can I just call that script somehow instead?

A really good way to do stuff with Django is to write management commands. They live in your Django apps, and allow you to add extra options to manage.py that have full access to your models etc. Highly recommended for scheduled tasks.

How would I schedule a Django management in pythonanywhere? Doesn't it still need to run the wsgi to set up its environment?

Ah, good question -- are you using a virtualenv? If so, it might be a bit more complicated, you'd need to activate it before running the management command.