Forums

Django scheduled task not working (no module name wsgi)

My .py script works fine when executed from the "Bash console in virtualenv django17" like this:

$ python regenerate-cache.py

But when executing the script as a scheduled task it produces the following error:

Traceback (most recent call last):
  File "/home/jorisvanleeuwen/veggi/regenerate-cache.py", line 4, in <module>
    from django.core.wsgi import get_wsgi_application
ImportError: No module named wsgi

Is django not supported in scheduled scripts? Thank you for your time!

Loading a wsgi application in a scheduled task doesn't make much sense. You probably don't need to do that. The other issue is that the virtualenv is not activated in scheduled tasks unless you do that. The easiest way is to make your scheduled task run the python executable in the bin directory in your virtualenv.

Thank you for your answer! How would I make the scheduled task run the regenerate-cache.py python executable in the bin directory in my virtual environment? Thanks a bunch!

In the "path" bit of the scheduled task, instead of specifying only the Python file, put the path to the Python interpreter first. So, for example, if your virtualenv is in the normal place and it's called django17, it would look something like this:

/home/jorisvanleeuwen/.virtualenvs/django17/bin/python  /home/jorisvanleeuwen/veggi/regenerate-cache.py

Sweet! That worked instantly, thank you for the help!