Sendgrid-python: When run with your quick start sample with python3.6, got No module named 'urllib2'

Created on 17 Jun 2018  路  3Comments  路  Source: sendgrid/sendgrid-python

Issue Summary

Your lib cannot run under python3.6

Steps to Reproduce

  1. Copy & Paste your sample script
  2. Replace API key, email etc
  3. Run it with Python3.6

Error Trace
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/python_http_client/client.py", line 7, in
import urllib.request as urllib
File "/usr/local/lib/python3.6/urllib/request.py", line 86, in
import email
File "/workspace/src/py/email.py", line 5, in
from sendgrid.helpers.mail import *
File "/usr/local/lib/python3.6/site-packages/sendgrid/helpers/mail/__init__.py", line 9, in
from .email import Email
File "/usr/local/lib/python3.6/site-packages/sendgrid/helpers/mail/email.py", line 4, in
import email.utils as rfc822
ModuleNotFoundError: No module named 'email.utils'; 'email' is not a package

During handling of the above exception, another exception occurred:

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

Technical details:

stackoverflow link for the reason

  • sendgrid-python Version: master (latest commit: [commit number])
  • Python Version: 3.6
unknown or a help wanted question

Most helpful comment

I ran into this issue myself. I also had an file named 'email.py' so I renamed it, uninstall sendgrid and reinstalled. Everything worked after that.

All 3 comments

I had a similar issue, but was able to solve it. I had a package named email.py that broke urllib. When their http client is importing other modules, if it fails to import urllib, it has an exception to import urllib2 (for backwards compatibility with Python 2).

Go to these lines in the http client code, and remove the try/except just leaving the Python 3 import statements.

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

That will tell you where your error actually is and why it isn't successfully importing.

Thanks @isaacnicholas!

@chenzhendong,

Have you tried @isaacnicholas' suggestion?

I ran into this issue myself. I also had an file named 'email.py' so I renamed it, uninstall sendgrid and reinstalled. Everything worked after that.

Was this page helpful?
0 / 5 - 0 ratings