Forums

Script ran perfectly with manual start; syntax error when run as task

This one is a puzzler....

What Do I Expect?

A program that runs perfectly in Python Anywhere console (or PyCharm for that matter) should run as a Python Anywhere task successfully.

What Is Happening?

When I run this same file as a task however I get the following error:

File "Birthday_Wisher/day_032_Birthday_Wisher.py", line 25 email_connection.sendmail(from_addr=MY_EMAIL, to_addrs=to_email, msg=f"Subject:Happy Birthday!\n\n{email_message}")

^ SyntaxError: invalid syntax

2021-05-25 22:10:58 -- Completed task, took 5.79 seconds, return code was 1.

The thing is, the above error does not make sense given that the code works in two different consoles with unchanged files or code. If this was a valid error it wouldn't run in PC or PA console. Something is off and this is not working as expected.

The function in question is as follows. Posting the entire program is moot as it is working with multiple files and works perfectly in two other environments, including the PA console. The code is not broken. Task automation is not playing nice with this code though...

def email_letter(email_message, to_email):
    """Receives the finalized email message and uses it to send the birthday email.
    Error handling will return a 'somewhat' coherent error code and message."""
    global birthday_list
    global emails_sent
    try:
        with smtplib.SMTP(MY_SMTP, port=587) as email_connection:
            email_connection.starttls()
            email_connection.login(user=MY_EMAIL, password=MY_PASSWORD)
            email_connection.sendmail(from_addr=MY_EMAIL, to_addrs=to_email, msg=f"Subject:Happy Birthday!\n\n{email_message}")
    except smtplib.SMTPResponseException or smtplib.SMTPRecipientsRefused as error:
        print(f"The automated birthday email to {to_email} failed to send.")
        print(f"Error code: {error.smtp_code}. Error message: {error.smtp_error}")
    else:
        emails_sent += 1

You are running your scheduled task with the wrong version of python. There are many ways to fix it: see https://help.pythonanywhere.com/pages/ScheduledTasks/

Thanks. The first task will likely be to identify which parts of the code are beyond 3.6. I've been coding in a 3.9 interpreter (learning for the past month) and don't have any experience with prior versions. This will be a good learning experience for me.

This does prompt a question though. Why are the versions of Python different between the regular Python Anywhere console and the Python Anywhere task scheduler?

This question is inspired by my experience. This code works in the Python Anywhere console, and yet it errors in the task scheduler. Regardless of versioning of the interpreter that is an odd situation from where I sit.

This does prompt a question though. Why are the versions of Python different between the regular Python Anywhere console and the Python Anywhere task scheduler?

How are you starting the console? If it's started by hitting ">>>Run" button in the in-browser editor, it would be a Python console and the Python version used would be the version specified in your account settings (-> System image -> Default Python Versions) or the version specified in the scripts shebang ("#!/..." at the beginning of the file). If, however, you start a regular Bash console, the default Python would be Python 2.7 because it's the default Python on Ubuntu we're using -- unsless you're in a virtual environment. I admit it might be a little bit confusing at the beginning (and it will be improved in the next system image) but the bottom line is -- the scheduled task runner would use the same default Python as in the regular Bash console, which is what should be expected.

The version in my account settings is 3.8. I start the console through:

  • Click on Dashboard
  • Click on the file
  • Click on >>> Run

This path appears to follow the what is advised. I am NOT running through the Bash console.

However, the question is now answered. I will paraphrase to confirm understanding. I can use this service to manually run a file. However, the scheduled task runner uses the same default Python version as the regular Bash console, which is version 2.7.

My conclusion is that if I wish to use Python Anywhere at this point in time to schedule scripts for Python I am limited to version 2.7.

Thanks for the answer. I look forward to the day when the version for the scheduled task runner is updated to > 3.0. Until then, I will modify my code to be compatible with v2.7.

My conclusion is that if I wish to use Python Anywhere at this point in time to schedule scripts for Python I am limited to version 2.7.

No, you're not. You can use all versions availabale on your system image. See: https://help.pythonanywhere.com/pages/ScheduledTasks/

Now I am utterly confused.

From the beginning I have been trying to communicate that when I run a script through the console it works perfectly. But it does not run in the task scheduler.

I am sorry, but my experience is counter to this. I have built a simple automation script that runs perfectly in PyCharm and the console currently at 3.8. I then try to run it through the task scheduler and it errors out.

And the assumption appears to be that I am somehow doing something wrong. At no point has anyone even entertained that there might be a bug, just that it is a user error.

Well, here's the thing. While I appreciate that people are taking time out of their day to help, I would invite those replying to fully read what is being posted. In the very first post I wrote, "The thing is, the above error does not make sense given that the code works in two different consoles with unchanged files or code. If this was a valid error it wouldn't run in PC or PA console. Something is off and this is not working as expected."

I specifically wrote that I was trying this in the PA console. I then just replied again with the specific steps to demonstrate that I was running this script in the console.

And now, I am left confused. The script works great in the PA console under 3.8. It works great in PyCharm 3.9. And it does not run in the task scheduler.

I guess this conversation is done.

A lot of servers work like this...it's not just a PythonAnywhere thing...your scheduled task command should look like this:

 python3.8 /home/myusername/myproject/myscript.py

If you don't specify the python version, the system (on or off PA) defaults to a set version of Python. The reason this works in PyCharm is that that value is the latest (and probably only) interpreter you have installed.

Your console runs on the newest version of Python because that is set when the console runs. Running a scheduled task is much like running a task on ubuntu server...if you don't specify, it will run Python 2.7. They have 2.7 installed also for compatibility reasons (e.g. people that have large codebases and/or haven't had the time to update low priority projects).

To simplify, this about it like this...opening a console more similar to what you are using in PyCharm. Running a scheduled task is more like running a task through cron on a server...it will default if not specified. Please see the page provided by pafk (this is pretty much the same info).

Hope this helps.

Also...PA is batteries included, but using a virtualenv helps prevent code breakage when packages are updated (not that code breakage always happens). More here.

Exactly like @tylerhand explained.