Forums

Sendgrid:

I am following the basic tutorial for using sendgrid that can be found here: https://app.sendgrid.com/guide/integrate/langs/python

I have a file called test_email.py that uses the following starter code:

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

 message = Mail(
    from_email='jonathan@jonathanbech.tel',
    to_emails='jonathanbechtel@gmail.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)

I installed it in pip, but if I run python test_email.py from my bash console I get the following Traceback:

Traceback (most recent call last):
File "/home/jonathanbechtel/.virtualenvs/my-virtualenv/lib/python3.7/site-packages/sendgrid/helpers/mail/email.py", line 2, in <module>
import rfc822
ModuleNotFoundError: No module named 'rfc822'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
 File "test_email.py", line 5, in <module>
  from sendgrid import SendGridAPIClient
  File "/home/jona/.virtualenvs/my-virtualenv/lib/python3.7/site-packages/sendgrid/__init__.py", line 19, in 
   <module>
  from .helpers.mail import *  # noqa
  File "/home/jona/.virtualenvs/my-virtualenv/lib/python3.7/site-packages/sendgrid/helpers/mail/__init__.py", line 4, in <module>
  from .bcc_email import Bcc
  File "/home/jona/.virtualenvs/my-virtualenv/lib/python3.7/site-packages/sendgrid/helpers/mail/bcc_email.py", line 1, in <module>
  from .email import Email
  File "/home/jona/.virtualenvs/my-virtualenv/lib/python3.7/site-packages/sendgrid/helpers/mail/email.py", line 4, in <module>
  import email.utils as rfc822
  File "/home/jona/personalwebsite/email.py", line 5, in <module>
  from sendgrid import SendGridAPIClient
  ImportError: cannot import name 'SendGridAPIClient' from 'sendgrid'`

However, running pip freeze reveals that I do in fact have it installed in my virtual environment. On the help page it says it works for 3.4 and 3.5, but I believe this is not an issue going by the thread on the company's github repo: https://github.com/sendgrid/sendgrid-python/issues/787

On a somewhat related note, I have a problem importing pandas from the console, even though it's installed in my virtual environment and is used on an app

I can't access the help page you're referring to -- it requires an account. Did you try following their suggested steps and use eg. Python 3.5 (just for experiment sake). And just for ruling out some strange scenarios -- did you edit the traceback message? It's weird that some paths have your home directory and some refer to other account -- jona.

What errors do you get regarding pandas?