如何修复:modulenotfounderror:no模块名为' config#x27;
我已经看过过去的线程,但据我所知,这些解决方案对我来说都没有。我的代码读取:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在观看的教程告诉您在其中创建一个名为
config.py
的文件,并将其定义放在其中的email_address
和email_passwordword
。但是,您似乎将这些定义与其他所有内容相同。这还可以,但是如果您要这样做,则无需将
导入config
放在代码的顶部。您也不需要在每次引用变量的名称之前添加config。
。工作代码看起来像这样:
但是,在Python脚本中放置诸如密码之类的敏感信息并不是一个好主意,以防您与其他人共享它们,而忘记了您已经这样做。
教程建议创建一个称为
config.py
存储此单独的python文件,但这实际上不是通常的方法。隐藏信息的通常方法将其放在与主脚本同一目录中的名为
.env
的文件中,并将信息从代码中取出。使用
.env
文件是因为它是存储环境变量的常规方式。如果您想执行此操作(这可能是最好的选择),那么这就是您要做的:
.env
的文件,然后填写信息如下:(请注意,您不需要语音标记。)
dotenv
模块:要查找有关
dotenv
的更多信息,请参见他们的官方pypi页。The tutorial you are watching tells you to create a file called
config.py
, and put the definitions ofEMAIL_ADDRESS
andEMAIL_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 putconfig.
before the names of the variables each time you reference them.The working code looks like this:
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:
.env
in the same directory as your main script, and fill it out with the information as follows:(Note that you do not need speech marks.)
dotenv
module using the following command:To find out more information about
dotenv
, see their official PyPI page.