Forums

scheduled tasks question

when start script through schedulet tasks there's import error:

Traceback (most recent call last):

  File "/home/kerniexvid/Topomac/Wacorund/main_checkCircle.py", line 11, in <module>

    from Wacorund.word_freq_counter import WordFreqCounter

ImportError: No module named Wacorund.word_freq_counter


2013-04-07 07:00:34 -- Completed task, took 2.00 seconds, return code was 1.

and no error when running this script from console

?

and with some other script through scheduled tasks theres "Permission denied" error

We need to narrow down what is different about the environment.

  • Which interpreter are you using:

    1. When you run from the console.
    2. When running as a scheduled task.
  • Permissions error is likely due to not having the execute bit set for the script.

    1. This may help.

it's default interpreter: python 2.7.3 inside script's file theres hashbang like #!/usr/bin/python also

Yeah, it sounds like the first case is a path issue. You'll need to do something like

import sys
sys.path.append("/home/kerniexvid/Topomac")

before you try and import any of the code inside the Wacorund module.

If you get permission denied then a chmod a+x the_script_you_want_to_run.py

I did this at top of that script:

sys.path.append("./")
sys.path.append("../")

and i thought its eqvivalent of:

sys.path.append("/home/kerniexvid/Topomac/")
sys.path.append("/home/kerniexvid/Topomac/Wacorund/")

fact that through console yes, the same, but with schedule tasks its not; mystery

The entries . and .. are equivalent to the current working directory and its parent - note this is not the same as the directory where the script file is located. A script can be invoked from any working directory, it's good practice not to rely on it being a particular location.

Ideally you just import your libraries and rely on the calling environment to have set PYTHONPATH correctly. However, since this isn't always convenient, you can obtain the directory of the current script file with:

os.path.dirname(os.path.abspath(__file__))

yep, what Cartroo said. we use that little os.path.dirname(__file__) trick all the time, eg in django settings files for fully specifying paths...