Forums

referencing modules in scheduled tasks

Having a couple of scripts with redundant functions, I've created myLib.py and import the module.

from myLib.py import myFunction

Runs without problem from bash. Module not found when run as a scheduled task. How should I do?

Do you use cd to get to a particular directory when you run the code from bash? If so, you'll need to do the same thing in your scheduled task in order to make sure that the Python module search path is correct. So, for example, if you run your script in bash like this:

cd /home/gauvins/somedirectory
python3.6 myscript.py

...then the scheduled task should be this:

cd /home/gauvins/somedirectory; python3.6 myscript.py

Note the semicolon separating the two commands.

No I don't specify a path since myLib resides in the same directory as the script that is calling it. AFAIK, python should search this folder as well as PATH. Which is probably why it works in bash and not as a scheduled task.

The proper (bash) syntax should look something like :

from home.userid.scriptFolder.lib import someFunction

where the scriptFolder contains _init_.py

Before I try this and variants, maybe you can confirm that the scheduled tasks directory will play nice with this syntax.

take care

No, you shouldn't need to change your code to make it work in a scheduled task -- and adding home.username at the start won't help, because those directories aren't Python modules.

Can I take a look at your code? We can see it from our admin interface, but we always ask for permission first.

let me try a couple of things first and get back to you. (I've realized that I was running the scripts with the run command of your files interface rather that in a bash environment per se).

OK -- let us know if you need any more help.