如何修复:modulenotfounderror:no模块名为' config#x27;

发布于 2025-02-04 18:55:21 字数 857 浏览 1 评论 0原文

我已经看过过去的线程,但据我所知,这些解决方案对我来说都没有。我的代码读取:

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 ツ")

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 ツ")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城花音 2025-02-11 18:55:21

您正在观看的教程告诉您在其中创建一个名为config.py的文件,并将其定义放在其中的email_addressemail_passwordword。但是,您似乎将这些定义与其他所有内容相同。

这还可以,但是如果您要这样做,则无需将导入config放在代码的顶部。您也不需要在每次引用变量的名称之前添加config。

工作代码看起来像这样:

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 :(")

但是,在Python脚本中放置诸如密码之类的敏感信息并不是一个好主意,以防您与其他人共享它们,而忘记了您已经这样做。

教程建议创建一个称为config.py存储此单独的python文件,但这实际上不是通常的方法。

隐藏信息的通常方法将其放在与主脚本同一目录中的名为.env的文件中,并将信息从代码中取出。

使用.env文件是因为它是存储环境变量的常规方式。

如果您想执行此操作(这可能是最好的选择),那么这就是您要做的:

  1. 在与主脚本同一目录中创建一个称为.env的文件,然后填写信息如下:
      email_address = ***
    email_address2 = ***
    email_password = ***
     

    (请注意,您不需要语音标记。)

  2. 使用以下命令安装dotenv模块:
      pip安装python-dotenv
     
  3. 修改您的代码以从环境文件中获取值,而不是定义脚本中的变量:
     导入smtplib
    导入操作系统
    来自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(主题,msg):
        尝试:
            server = smtplib.smtp(“ smtp-mail.outlook.com:587”)
            server.ehlo()
            server.starttls()
            server.login(email_address,email_password)
            消息= f“主题:{主题} \ n \ n {msg}”
            server.sendmail(email_address,email_address2,邮件)
            server.quit()
            打印(“电子邮件发送ツ”)
        除了:
            打印(“电子邮件未能发送:(”)
     

要查找有关dotenv的更多信息,请参见他们的官方pypi页

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文