简体   繁体   English

使用 os.environ 访问环境变量?

[英]Using os.environ to access environment variables?

I'm trying to access the email address and email password of the current user environment but when I try to print the herein variables 'EMAIL_ADDRESS' and 'EMAIL_PASSWORD', I get None values.我正在尝试访问当前用户环境的 email 地址和 email 密码,但是当我尝试打印此处的变量“EMAIL_ADDRESS”和“EMAIL_PASSWORD”时,我得到 None 值。

I'm using an Android phone to code this simple project.我正在使用 Android 电话来编写这个简单的项目。 Here is it:就这个:

import os
import smtplib

EMAIL_ADDRESS = 
os.environ.get('EMAIL_USER')
EMAIL_PASSWORD = os.environ.get('EMAIL_PASS')


subject = 'Practice email code'
body = 'This is my 1st output'
msg = f'Subject:{subject}\n{body}'



server = 
smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)




server.sendmail(EMAIL_ADDRESS, EMAIL_PASSWORD, msg)
server.close()

Almost similar questions I've found here in StackOverflow have received a suggestion to make a new file containing the email ad and password.我在 StackOverflow 中发现的几乎类似的问题都收到了建议,建议制作一个包含 email 广告和密码的新文件。 But is there a way to not cross with this solution?但是有没有办法不与这个解决方案交叉? If none, what's the best you could recommend for a beginner to approach this problem?如果没有,您可以为初学者推荐解决此问题的最佳方法是什么? I don't have any much goal as I am yet to have a solid introductory skill.我没有任何目标,因为我还没有扎实的入门技能。 So as long as I could send the email through using the object 'os.environ' to access my email ad and password, I'm good to go. So as long as I could send the email through using the object 'os.environ' to access my email ad and password, I'm good to go.

Any suggestion will be appreciated!任何建议将不胜感激!

That is not Python's problem.这不是 Python 的问题。 Try writing bash script to see if the two variables are really defined.尝试编写bash脚本,看看是否真的定义了这两个变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM