Cannot run SendGrid Python

199 Views Asked by At

I'm using the SendGrid sample code to make sure my Token is ready, but every time I try to run it I get an error with RFC822 My Sample Code:

import os
import sendgrid
from sendgrid.helpers.mail import Mail

message = Mail(
    from_email='[email protected]',
    to_emails='[email protected]',
    subject='Sending with Twilio SendGrid is Fun',
    html_content='<strong>and easy to do anywhere, even with Python</strong>')
try:
    sg = sendgrid.SendGridAPIClient('some token')
    response = sg.send(message)
    print(response.status_code)
    print(response.body)
    print(response.headers)
except Exception as e:
    print(e.message)

I always get this error:

ModuleNotFoundError: No module named 'rfc822'

During handling of the above exception, another exception occurred:
ImportError: cannot import name 'Mail' from partially initialized module 'sendgrid.helpers.mail' (most likely due to a circular import) (/home/userrrr/.local/lib/python3.8/site-packages/sendgrid/helpers/mail/__init__.py)

I'm using python3 and pip3, also tried older versions of sendgrid with no hope, also tried it on Windows and Ubuntu

Any ideas? Thank You !

1

There are 1 best solutions below

0
Kevin Qi On

I fixed this by running the code as a module (e.g. python -m myapp.sendgrid_script) instead of executing the script with python sendgrid_script.py.