Home > OS >  How to fix: ModuleNotFoundError: No module named 'config'
How to fix: ModuleNotFoundError: No module named 'config'

Time:06-07

I've looked at past threads but none of the solutions are working for me as far as I know. My code reads:

import smtplib
import config
EMAIL_ADDRESS = '***'
EMAIL_PASSWORD = '***'
                    
                    if re.search(pattern, body) is None:
                        def send_email(subject, msg):
                            try:
                                server = smtplib.SMTP('smtp-mail.outlook.com:587')
                                server.ehlo()
                                server.starttls()
                                server.login(config.EMAIL_ADDRESS, config.EMAIL.PASSWORD)
                                message = 'Subject: {}\n\n{}'.format(subject, msg)
                                server.sendmail(config.EMAIL_ADDRESS, '***@gmail.com', message)
                                server.quit()
                                print("email sent ツ")

CodePudding user response:

The tutorial you are watching tells you to create a file called config.py, and put the definitions of EMAIL_ADDRESS and EMAIL_PASSWORD in there. However, you seem to be putting these definitions in the same script as everything else.

This is OK, but if you are going to do this, you do not need to put import config at the top of your code. You also don't need to put config. before the names of the variables each time you reference them.

The working code looks like this:

import smtplib

EMAIL_ADDRESS = "***"
EMAIL_ADDRESS2 = "***"
EMAIL_PASSWORD = "***"


def send_email(subject, msg):
    try:
        server = smtplib.SMTP("smtp-mail.outlook.com:587")
        server.ehlo()
        server.starttls()
        server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
        message = f"Subject: {subject}\n\n{msg}"
        server.sendmail(EMAIL_ADDRESS, EMAIL_ADDRESS2, message)
        server.quit()
        print("email sent ツ")
    except:
        print("Email failed to send :(")

However, it is not really a good idea to put sensitive information like passwords in Python scripts, in case you share them with someone else, forgetting that you have done so.

The tutorial suggests creating a separate Python file called config.py to store this, but this is not actually the usual way it is done.

The usual method of hiding information is putting it in a file called .env in the same directory as the main script, and taking the information out of there in the code.

The .env file is used because it is the conventional way of storing environment variables.

If you want to do this (which is probably the best option), then here is what you have to do:

  1. Create a file called .env in the same directory as your main script, and fill it out with the information as follows:
    EMAIL_ADDRESS=***
    EMAIL_ADDRESS2=***
    EMAIL_PASSWORD=***
    
    (Note that you do not need speech marks.)
  2. Install the dotenv module using the following command:
    pip install python-dotenv
    
  3. Modify your code to take values from the environment file instead of defining the variables in the script:
    import smtplib
    import os
    from dotenv import load_dotenv
    
    load_dotenv()
    EMAIL_ADDRESS = os.getenv("EMAIL_ADDRESS")
    EMAIL_ADDRESS2 = os.getenv("EMAIL_ADDRESS2")
    EMAIL_PASSWORD = os.getenv("EMAIL_PASSWORD")
    
    
    def send_email(subject, msg):
        try:
            server = smtplib.SMTP("smtp-mail.outlook.com:587")
            server.ehlo()
            server.starttls()
            server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
            message = f"Subject: {subject}\n\n{msg}"
            server.sendmail(EMAIL_ADDRESS, EMAIL_ADDRESS2, message)
            server.quit()
            print("email sent ツ")
        except:
            print("Email failed to send :(")
    

To find out more information about dotenv, see their official PyPI page.

  • Related