Forums

Can't insert object to MySQL with scheduled tasks

I have a simple function that inserts objects to MySQL (with Peewee ORM), it works perfectly on my pc and in the Python Anywhere console too, but unfortunately if I try to run the same file as a scheduled task, I'm getting an error like this:

tablename.insert_many(objdict).execute()
AttributeError: type object tablename' has no attribute 'insert_many'

Since my implementation runs without any problems from the PA console, I'm sure it's something related to the sceduled tasks, but I have no idea what can cause the problem. I would really appreciate if somebody could help me to figure out what's wrong.

The method that crashes (at scheduled tasks) looks like this:

def uploadObject (objDict):
    tablename.insert_many(objDict).execute()

I took a look at the command you are using for your scheduled task and I think you are using python3 for your consoles but python2 for your scheduled tasks.

Specify the python version for your scheduled task by either python3.4 path-to-my-task, or if you are using a virtualenv, use the path to your virtualenv python instead of just python3.4.

EDIT:Got it! Thanks the fast answer :) python3.4 /home/username/projectfolder/subfolder/app.py

Thank you Conrad! Please could you tell me how should I specify the Python version in my path? I made a fast search, but couldn't figure it out. I've tried these options (I'm not using virtualnv):

 /home/username/python3.4/projectfolder/subfolder/app.py
 /python3.4/ /home/username/projectfolder/subfolder/app.py

That's exactly right: python3.5 (or python3.5 or python3.3) is how you specify the Python version. If you want to be super-explicit, you can use the full path to the Python binary, which you can find by running which python3.4 in a bash console.