Forums

Unable to import other python modules in scheduled task python file

I've tried the options listed on this link (https://help.pythonanywhere.com/pages/VirtualEnvInScheduledTasks) but no matter what I do I get the below error when the task runs:

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Not sure how I can import my other code into the scheduled task py file?

If you want to run Django code from outside your website, you need to do some setup first so that the framework can find all of the appropriate stuff. The recommended way to do that is to write it as a Django management command. That link goes to the docs for Django 2.0, but there's a selector near the bottom right where you can find the documentation for other versions.

Thank you for the response! I tried that but when I try to run a manage.py command (even one of the standard ones offered by django and not the new one I added) it won't let me create the task, the task page just gives me the message "CSRF failed: CSRF token missing or incorrect". I typed in what I usually type into the bash console: "python /home/myusername/myproject/manage.py command".

That's odd. Could you try reloading the page, and see if you can add the task? It sounds like you might have been logged out in the background.

Thank you! It's working now!

Excellent! Glad I could help :-)