sendgrid-python icon indicating copy to clipboard operation
sendgrid-python copied to clipboard

ImportError: cannot import name 'SendGridAPIClient'

Open mabodx opened this issue 7 years ago • 15 comments

Issue Summary

A summary of the issue and the environment in which it occurs. If suitable, include the steps required to reproduce the bug. Please feel free to include screenshots, screencasts, code examples.

I pip install the sendgrid. image

I just follow your guide in this link https://app.sendgrid.com/guide/integrate/langs/python and when I run the example code following, I get the error below

python version: Python 3.6.1 :: Anaconda custom (64-bit). computer: mac10.14 CODE

# using SendGrid's Python Library
# https://github.com/sendgrid/sendgrid-python
import os
from sendgrid import SendGridAPIClient
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 = 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)

ERROR

Traceback (most recent call last):
  File "/Users/mabodx/anaconda/lib/python3.6/site-packages/python_http_client/client.py", line 7, in <module>
    import urllib.request as urllib
  File "/Users/mabodx/anaconda/lib/python3.6/urllib/request.py", line 86, in <module>
    import email
  File "/Users/mabodx/code/Paritko/anguis/email/email.py", line 4, in <module>
    from sendgrid import SendGridAPIClient
ImportError: cannot import name 'SendGridAPIClient'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "email.py", line 4, in <module>
    from sendgrid import SendGridAPIClient
  File "/Users/mabodx/anaconda/lib/python3.6/site-packages/sendgrid/__init__.py", line 19, in <module>
    from .sendgrid import SendGridAPIClient  # noqa
  File "/Users/mabodx/anaconda/lib/python3.6/site-packages/sendgrid/sendgrid.py", line 19, in <module>
    import python_http_client
  File "/Users/mabodx/anaconda/lib/python3.6/site-packages/python_http_client/__init__.py", line 1, in <module>
    from .client import Client  # noqa
  File "/Users/mabodx/anaconda/lib/python3.6/site-packages/python_http_client/client.py", line 12, in <module>
    import urllib2 as urllib
ModuleNotFoundError: No module named 'urllib2'

Steps to Reproduce

  1. This is the first step run this example code
# using SendGrid's Python Library
# https://github.com/sendgrid/sendgrid-python
import os
from sendgrid import SendGridAPIClient
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 = 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)```

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

Any other information you want to share that is relevant to the issue being reported. Especially, why do you consider this to be a bug? What do you expect to happen instead?

#### Technical details:

* sendgrid-python Version: master (latest commit: [commit number])
* Python Version:  3.6.1 

mabodx avatar Apr 13 '19 10:04 mabodx

Hello @mabodx,

Can you please try pip install --upgrade sendgrid?

Thanks!

With Best Regards,

Elmer

thinkingserious avatar Apr 16 '19 15:04 thinkingserious

If you upgrade it still returns the same error

AspieCoder1 avatar Apr 21 '19 10:04 AspieCoder1

Hello @sai109,

It looks like the error is happening here, but that should not get executed unless there was a problem with the following imports:

import urllib.request as urllib
from urllib.parse import urlencode
from urllib.error import HTTPError 

Could you please try running those commands in the python interpreter and let me know what errors you are seeing?

I just tried on Python 3.6.3 without any error.

Thanks!

With Best Regards,

Elmer

thinkingserious avatar Apr 22 '19 20:04 thinkingserious

Thank you for the response. When I run the import from the python interpreter they work. But if I run the code in the python file I still get the error: Code (email.py)

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

message = Mail(
    from_email="from__email",
    to_emails="to__email__",
    subject="Cambridge Computer Science Admissions Updated",
    html_content="The admissions test information for cambridge has been updated"
)

try:
    sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    sg.send(message)
except Exception as e:
    print(e)

When I run on the console I get:

from sendgrid import SendGridAPIClient
ImportError: cannot import name 'SendGridAPIClient'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "email.py", line 2, in <module>
    from sendgrid import SendGridAPIClient
  File "C:\ProgramData\Anaconda3\lib\site-packages\sendgrid\__init__.py", line 19, in <module>
    from .sendgrid import SendGridAPIClient  # noqa
  File "C:\ProgramData\Anaconda3\lib\site-packages\sendgrid\sendgrid.py", line 19, in <module>
    import python_http_client
  File "C:\ProgramData\Anaconda3\lib\site-packages\python_http_client\__init__.py", line 1, in <module>
    from .client import Client  # noqa
  File "C:\ProgramData\Anaconda3\lib\site-packages\python_http_client\client.py", line 12, in <module>
    import urllib2 as urllib
ModuleNotFoundError: No module named 'urllib2'

Python Version: Anaconda (Python 3.6.6)

AspieCoder1 avatar Apr 26 '19 06:04 AspieCoder1

Can you try renaming your file from email.py to email_test.py?

thinkingserious avatar Apr 30 '19 17:04 thinkingserious

I get a similar error on Mac OS. When I tried to rename my file from email.py to something else, another message poped up:

Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1318, in do_open encode_chunked=req.has_header('Transfer-encoding')) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1239, in request self._send_request(method, url, body, headers, encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1285, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1234, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1026, in _send_output self.send(msg) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 964, in send self.connect() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1400, in connect server_hostname=server_hostname) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 407, in wrap_socket _context=self, _session=session) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 814, in init self.do_handshake() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 1068, in do_handshake self._sslobj.do_handshake() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ssl.py", line 689, in do_handshake self._sslobj.do_handshake() ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "email_test.py", line 12, in response = sg.send(message) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sendgrid/sendgrid.py", line 98, in send response = self.client.mail.send.post(request_body=message.get()) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/python_http_client/client.py", line 252, in http_request return Response(self._make_request(opener, request, timeout=timeout)) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/python_http_client/client.py", line 172, in _make_request return opener.open(request, timeout=timeout) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 526, in open response = self._open(req, data) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 544, in _open '_open', req) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 504, in _call_chain result = func(*args) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1361, in https_open context=self._context, check_hostname=self._check_hostname) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 1320, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:777)>

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "email_test.py", line 17, in print(e.message) AttributeError: 'URLError' object has no attribute 'message'

croozeus avatar May 30 '19 13:05 croozeus

Hello @croozeus,

Please try print(str(e)).

That said, it looks like you need to execute some additional steps on your machine.

thinkingserious avatar Jun 03 '19 22:06 thinkingserious

hola buen dia, disculpen alguien me podria ayudar con un problema que me aparece en spyder. estoy estableciendo una conexión con v-rep para una simulación de movimientos y al momento de cargar el codigo me aparece lo siguiente:

runfile('C:/Users/Luis Gerardo/Downloads/python-Vrep/g_population.py', wdir='C:/Users/Luis Gerardo/Downloads/python-Vrep')

runfile('C:/Users/Luis Gerardo/Downloads/python-Vrep/prueba.py', wdir='C:/Users/Luis Gerardo/Downloads/python-Vrep') [[-4, 2, 1, 5], [-1, 5, -2, -4], [-3, 1, -4, 2], [5, 3, -4, -2], [1, -4, -5, -3], [-3, 1, 2, -4], [2, 3, -1, -4], [-5, -4, -1, 3], [3, -2, -1, 5], [-3, 2, -1, -4], [-1, -5, -4, 3], [-5, 1, 4, -3], [1, 4, -2, -3], [-1, 2, -4, 5], [-1, -3, -4, 5], [2, 5, -1, 3], [-4, 5, 1, 3], [1, -3, 2, -5], [5, 3, -4, -2], [-2, -1, -5, -4], [5, -1, 4, -2], [-2, -3, -4, -1], [3, -4, -2, 5], [-3, 1, -5, -2], [5, 1, 3, 4], [4, -3, 5, 1], [-1, -5, -2, 4], [-3, 4, 2, -5], [-4, -3, 2, 1], [-2, 1, -3, 4], [1, -5, -4, 3], [-4, 2, 5, -3], [4, 2, 5, 1], [2, -3, -4, 5], [5, 3, -2, -1], [-2, -3, -5, -1], [5, 2, 4, 1], [-4, 2, 1, 3], [-1, 4, -2, -3], [-3, 5, -4, 2], [-5, -3, -1, 2], [1, -3, -5, 4], [1, -3, -4, -5], [-1, 5, 3, 2], [2, -3, 5, -4], [2, 5, -1, 4], [2, 4, 3, -5], [-2, 4, -3, 5], [3, -1, 5, 4], [5, 4, 3, -2], [5, -3, 1, -2], [-2, 5, 3, 4], [-5, 3, -4, -1], [2, -3, -5, 1], [-3, -2, -1, -4], [3, 5, 1, 4], [4, 2, -5, -1], [-5, 4, -2, 3], [5, 1, 4, -3], [5, 1, 3, -4], [1, 5, -4, 3], [-3, -4, 1, 5], [5, 3, 2, 1], [1, 2, 4, 3], [1, -2, -5, 3], [-2, 1, -3, 5], [1, -2, -4, 3], [-5, 1, 3, 2], [-3, 5, 1, -2], [1, 4, -5, 3], [4, 2, -5, 1], [5, -1, 4, -3], [-5, -3, 1, 2], [1, 3, 4, -2], [3, 1, -4, 2], [2, -5, -1, -4], [2, -1, 4, 3], [4, 3, -5, 1], [-4, -3, -2, -1], [-5, -1, 4, 3], [-2, 4, 1, 5], [5, -3, 1, -4], [-2, -4, 5, -1], [4, 3, 2, 1], [2, 3, -4, 1], [3, 1, -5, -2], [2, 3, 1, -4], [3, 1, -2, 5], [-5, 1, 4, -2], [-5, -1, -2, -3], [-2, 3, 5, -1], [-2, -5, -4, 3], [-1, 2, 3, 5], [2, 4, 1, -5], [3, -1, -4, 5], [4, -2, -3, -5], [-2, -1, 4, -5], [4, -5, -3, -1], [4, -1, 5, 3], [-5, -1, 4, -2], [4, -3, 2, 5], [4, -5, -1, 2], [1, -4, 3, 5], [-4, -3, -5, -1], [4, 2, -3, -5], [-3, -2, 1, -5], [-2, -3, -1, -4], [1, -5, 4, 3], [3, -5, -1, -2], [-1, 3, -4, -5], [-1, -2, 4, 3], [2, 5, 4, 1], [-4, 5, -1, 3], [-1, 4, 2, -3], [-4, -2, -3, -5], [4, 5, -3, 1], [-4, -5, -3, 2], [-3, 2, 1, 4], [-3, -2, -4, 1], [-1, 5, -4, 2], [-4, -1, -2, 3], [3, 5, -4, -1], [1, -3, -5, 2], [1, 3, -5, 4], [5, 2, 4, 3], [3, 4, 5, 1], [-2, -3, -5, -1], [-5, -3, 4, -2], [-1, -5, -4, -2], [3, 1, -5, -2], [2, 5, 1, -3], [-2, -3, -1, -4], [5, -4, 3, -1], [4, 1, -5, -3], [-5, -4, 3, 2], [1, -5, 4, 3], [3, 4, 2, -5], [4, -2, 3, -1], [-3, -1, -4, 2], [-1, -5, 4, 3], [5, 1, 3, -2], [-3, -5, -2, -1], [5, 3, 4, 1], [-2, -4, 1, -5], [4, -1, 2, 3], [-1, 3, -2, -5], [-1, -4, 3, -5], [1, 5, 4, 3], [5, 4, -2, 1], [4, -3, 2, -1], [4, -3, 1, 5], [-5, 3, -1, -2], [-2, 3, 1, 5], [3, 1, 2, -4], [2, -3, -4, -5], [-1, 5, 2, -4], [1, 2, -5, -3], [-3, 4, -2, 5], [-4, 5, -3, -2], [1, 5, -3, 2], [5, -2, 4, -1], [-3, -1, 2, 4], [1, -5, -4, 3], [4, -3, -1, -2], [4, 3, 1, -5], [4, 2, -1, -5], [5, 4, 3, 2], [-4, -5, -3, 2], [-4, -5, -2, -3], [5, -4, -2, 1], [-1, 3, -4, -2], [-2, -3, 5, -1], [3, 1, 5, -2], [-4, 1, -5, 3], [-4, -1, -3, -5], [3, -4, -2, 1], [5, 4, 2, 3], [-5, 1, -4, 2], [4, 1, -3, 2], [-1, -5, 4, -3], [4, 1, -3, -2], [5, -1, -2, 3], [1, -3, 4, -2], [-1, 5, 2, -4], [-5, 4, -2, 1], [3, 4, -5, -2], [2, -3, -5, 1], [3, 5, 2, 4], [-5, 2, -1, -3], [3, 5, -4, -1], [5, 3, 1, -4], [3, 5, -1, -2], [-1, 3, 4, 2], [-2, 1, -3, 4], [-5, 2, 1, 4], [-3, -5, 4, 1], [-1, 2, 3, 5], [2, 4, 5, 1], [-4, -1, 5, 2], [2, -3, 4, 5], [1, -3, 4, 2], [-3, -4, 2, -1], [2, 4, 5, 1], [-3, 5, 1, 2], [-1, 4, 3, 2], [1, -2, -5, -4], [-1, -2, -5, -3], [-4, 3, 2, 5], [-1, 3, 4, 2], [3, 4, -2, 1]]

runfile('C:/Users/Luis Gerardo/Downloads/python-Vrep/sistema_robots.py', wdir='C:/Users/Luis Gerardo/Downloads/python-Vrep') Reloaded modules: g_population Traceback (most recent call last):

File "C:\Users\Luis Gerardo\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code exec(code_obj, self.user_global_ns, self.user_ns)

File "", line 1, in runfile('C:/Users/Luis Gerardo/Downloads/python-Vrep/sistema_robots.py', wdir='C:/Users/Luis Gerardo/Downloads/python-Vrep')

File "C:\Users\Luis Gerardo\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile execfile(filename, namespace)

File "C:\Users\Luis Gerardo\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Luis Gerardo/Downloads/python-Vrep/sistema_robots.py", line 30 print ('Connected to remote API server') ^ IndentationError: unexpected indent

luisgeraro avatar Jul 09 '19 14:07 luisgeraro

pero el error se presenta en esta parte: runfile('C:/Users/Luis Gerardo/Downloads/python-Vrep/sistema_robots.py', wdir='C:/Users/Luis Gerardo/Downloads/python-Vrep') Reloaded modules: g_population Traceback (most recent call last):

File "C:\Users\Luis Gerardo\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code exec(code_obj, self.user_global_ns, self.user_ns)

File "", line 1, in runfile('C:/Users/Luis Gerardo/Downloads/python-Vrep/sistema_robots.py', wdir='C:/Users/Luis Gerardo/Downloads/python-Vrep')

File "C:\Users\Luis Gerardo\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile execfile(filename, namespace)

File "C:\Users\Luis Gerardo\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Luis Gerardo/Downloads/python-Vrep/sistema_robots.py", line 30 print ('Connected to remote API server') ^ IndentationError: unexpected indent

luisgeraro avatar Jul 09 '19 14:07 luisgeraro

Can you try renaming your file from email.py to email_test.py?

Renaming my python file for email.py to email_test.py stopped this issues happening

paultop6 avatar Jul 10 '19 09:07 paultop6

WHY? why does renaming it work??

timconnorz avatar Oct 04 '19 03:10 timconnorz

^ seconding @timconnorz. Renamed from email.py to email_test.py and it ran fine for some reason. Feels like a real-life xkcd.

daichiishi avatar Oct 10 '19 01:10 daichiishi

@thinkingserious Faced similar issue and renaming the file from email.py to email_test.py worked. Can you please take a moment to explain why it might be so?

mehuled avatar Dec 04 '19 12:12 mehuled

Ran into the same problem, also fixed by renaming my fail from email.py to mail.py. I think there is a module called email in Python that this library uses; when you call your own file email that module gets shadowed and breaks sendgrid. Might not be worth fixing, since it would probably be a lot of work just to save user from renaming, but it would be helpful to check for this specific case and print a more informative error.

metov avatar Dec 10 '19 19:12 metov

incredible. I have renamed my file "sendgrid.py" to "mail.py" and the issue stopped )))))

ykostivtribo avatar Jan 12 '21 21:01 ykostivtribo