1
votes

I want to send push notifications to my users hourly.

from push_notifications.models import APNSDevice
print('test schedule task')
device = APNSDevice.objects.all()
if device is None:
    print('None Device')
print('number of models is '+str(len(device)))
device.send_message('test')

Above is what I try to execute hourly.

But when executed that script, what i got is an error message :

"Traceback (most recent call last): File "/home/User/Folder/Project/App/schedule.py", line 1, in from push_notifications.models import APNSDevice ImportError: No module named push_notifications.models

2016-03-19 05:49:05 -- Completed task, took 0.00 seconds, return code was 1."

What should I do to solve this one?

print(sys.path)) is below :

['/home/User/Folder/Project/App', '/usr/local/lib/python2.7/dist-packages/snappy-2.3.2-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/cypari-1.2.2-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/pypng-0.0.18-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/FXrays-1.3.1-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/spherogram-1.4.1-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/plink-1.8-py2.7.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/local/lib/python2.7/dist-packages/Orange/orng', '/usr/local/lib/python2.7/dist-packages/PIL', '/usr/lib/python2.7/dist-packages', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode']

3
Are you sure push_notifications is installed?Sean Francis N. Ballais
@SeanFrancisN.Ballais Absolutely, I did succeed in functioning that function but with scheduled task.LKM
Is the file models.py inside /home/User/Folder/Project/App/push_notifications/? If this is Django I'd expect it to be in /home/User/Folder/Project/push_notifications/, in which case you'd need to add /home/User/Folder/Project/ to your sys.path.Giles Thomas

3 Answers

1
votes

You need to import the module at the beginning of your code in order for the program to call it.

1
votes

A bit late to the show, If you are executing this script hourly on pythonanywhere you don't have the same context as you would have when executing something like this through manage.py shell. I think you should turn this piece of code into a custom management command. https://docs.djangoproject.com/en/3.0/howto/custom-management-commands/

-1
votes

Take a look at your wsgi.py file and see how you define the environment variable there. You will probably have to do the same in the scheduled task to run it as a script.