sendgrid / sendgrid-python

The Official Twilio SendGrid Python API Library
https://sendgrid.com
MIT License
1.53k stars 711 forks source link

Partially initialized module, Circular Import Error #987

Closed aarontgit closed 3 years ago

aarontgit commented 3 years ago

Issue Summary

I am trying to integrate SendGrid using the WebAPI with Python, however I keep getting this error message: 'cannot import name 'SendGridAPIClient' from partially initialized module 'sendgrid' (most likely due to a circular import)'

Steps to Reproduce

  1. This is the first step
  2. This is the second step
  3. Further steps, etc.

Code Snippet

from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail

message = Mail(
    from_email='from_email@example.com',
    to_emails='to@example.com',
    subject='Sending with Twilio SendGrid is Fun',
    html_content='<strong>and easy to do anywhere, even with Python</strong>')
try:
    sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    response = sg.send(message)
    print(response.status_code)
    print(response.body)
    print(response.headers)
except Exception as e:
    print(e.message)

Exception/Log

# paste exception/log here

Technical details:

rlSimonLi commented 3 years ago

Is your python filename along the lines email.py or sendgrid.py? Try changing it to something else.

aarontgit commented 3 years ago

Is your python filename along the lines email.py or sendgrid.py? Try changing it to something else. That worked. However, I am now receiving a 'UnauthorizedError: HTTP Error 401: Unauthorized'

thinkingserious commented 3 years ago

Thanks for helping out @rlSimonLi!

@aarontgit please check that your environment variable SENDGRID_API_KEY is set correctly and that os.environ.get('SENDGRID_API_KEY') is returning the right value. If so, then there may be an issue with your account (in that case, please reach out to https://support.sendgrid.com). Thank you!