Set environment variables pycharm in (solving environmental variables os.environ.get not set)

本机环境:Mac+pycharm专业版

在flask web开发一书中,电子邮件使用的flask-mail库中,对于邮箱的账号和密码,作者使用的是环境变量:
app.config['MAIL_USERNAME']=os.environ.get('MAIL_USERNAME')
app.config['MAIL_PASSWORD']=os.environ.get('MAIL_PASSWORD')

Terminal Input terminal then:

export MAIL_USERNAME=*********@qq.com

export MAIL_PASSWORD=**************

Enter in a terminal:

echo $ MAIL_USERNAME
will find that you can output value, but any course problems when running the program, the problem is Miao Xu:

smtplib.SMTPSenderRefused: (503, b'Error: need EHLO and AUTH first !')

Then I joined the output of intermediate variables in the program:

#发邮件
def send_mail(to,subject,template,**kwargs):
    print(app.config['MAIL_USERNAME'])
    print(app.config['MAIL_PASSWORD'])
    print(FLASKY_MAIL_SENDER)
    msg = Message(app.config['FLASKY_MAIL_SUBJECT_PREFIX']+subject,
                  sender=FLASKY_MAIL_SENDER,recipients=[to])
    msg.body = render_template(template+'.txt',**kwargs)
    msg.html = render_template(template+'.html',**kwargs)
    thr = Thread(target=send_sasync_email,args=[app,msg])
    thr.start()
    return thr

While the end result of all three print None, that is, I use the environment variables set in the terminal program with

app.config['MAIL_USERNAME']=os.environ.get('MAIL_USERNAME')

And can not get.

After extensive dalao who saw the blog, I found that the problem lies not use environment variables defined in the pycharm (for unknown reasons, there dalao said it was because the process is the child process pycharm desktop environment, startup time will read terminal take .bashrc, set the environment variable is not configured at the system level, so pycharm do not see this environment variable)

Ever since we have to find ways to define an environment variable in pycharm in:

1. Preference -> Build, Execution, Deployment -> Console -> Python Console at the top can set the environment variable
Run 2. The top task bar -> Edit Configurations which has a Environment Variables

The method of FIG. 1 as follows:

In the introduction to the Environment variables.

Then again, print and email account password can find it already has a value, but the message can be normally sent.

 

Reference: https://youtrack.jetbrains.com/issue/PY-17816

Guess you like

Origin blog.csdn.net/hrbust_cxl/article/details/86771536