0

I wanted to send a mail with attachment, so first I was trying to send mail using python 3.6 and using pycharm IDE. Every time while running the file I get the following exception.

Traceback (most recent call last): File "/home/UDHAV.MOHATA/script/test.py", line 11, in <module> import smtplib File "/usr/lib/python3.6/smtplib.py", line 47, in <module> import email.utils File "/usr/lib/python3.6/email/utils.py", line 28, in <module> import random File "/home/UDHAV.MAHATA/script/random.py", line 1, in <module> import urllib3 ModuleNotFoundError: No module named 'urllib3' 

Sample code:

import smtplib import ssl subject = "An email with attachment from Python" body = "This is an email with attachment sent from Python" sender_email = "***********" receiver_email = "***************" password = "********" message = """\ Subject: Hi there This message is sent from Python.""" context = ssl.create_default_context() with smtplib.SMTP_SSL_PORT("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, password) server.sendmail(sender_email, receiver_email, message) 

Exact python version: 3.6.9

My motive is to send a mail with an attachment. If anyone knows the fix for this import exception or another way. Please let me know.

0

1 Answer 1

2

You named your file random.py, which causes import random in /usr/lib/python3.6/email/utils.py to import your own script instead of the standard lib random module. In turn, your own random.py file imports urllib3 which apparently is not installed on your interpreter.

Never name your script/module with the same name of a standard lib built-in/module.

To fix this error you will need to:

  • Change the name of your random.py.
  • Install the urllib3 so your script can import it (if you really need it)
Sign up to request clarification or add additional context in comments.

1 Comment

Its working now. I just realised I had another file named random.py, thats why it was failing. Thanks!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.