Forums

Code works in PyCharm and VsCode but doesn't in pythonanywhere

import random import smtplib import datetime as dt import pandas as pd

birthdayList = (pd.read_csv("birthdays.csv").to_dict(orient="index")).items()
now = dt.datetime.now()
winner = [person for (index, person) in birthdayList if (person["month"], person["day"]) == (now.month, now.day)]

for i in winner:
    chosen_letter = f"letter_{random.randrange(1,4)}.txt"
    with open(f"letter_templates/{chosen_letter}","r") as file:
        letter= file.read().replace("[NAME]",i['name'])
    email="xxxxxx@gmail.com"
    password="xxxxxxxx"

    with smtplib.SMTP("smtp.gmail.com") as connection:
        connection.starttls()
        connection.login(user=email, password=password)
        connection.sendmail(from_addr=email, to_addrs=i['email'], msg=f"Subject: Happy Birthday!!\n\n{letter}")

When I run this code anywhere else, it works as intended and sends an email, but when I try to run it here in pythonanywhere, it doesn't work.

I placed Print(winner) after the 'winner =... ' line. In pythonanywhere, it prints out as an empty list.

Pls help ;-

It sounds like it's not reading the birthdays.csv file as expected. Are you running your code with the working directory set to the directory where that file exists? You might want to try using a full path instead of a relative one -- for example /home/Sampannas/somedirectory/birthdays.csv

It is reading the birthday.csv. When I ask it to print birthdayList it prints it correctly.

Free accounts can only use gmail to send SMTP emails: https://help.pythonanywhere.com/pages/SMTPForFreeUsers/