简体   繁体   中英

Unable to declare environment variable using os.environ

When I tried setting environment variable value:

import os
os.environ['DB_PASSWORD'] = 'Password123'

on PyCharm CE 2020.2, it does not even set the environment variable values for the duration of the Python process or child processes. It gives me the error:

DB_PASSWORD
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'DB_PASSWORD' is not defined

Why is it so? What can be done to get this working without having to manually add the values?

Here's the screenshot to the error: https://the-inevitable-one.tumblr.com/image/627482222106165248

os.environ['DB_PASSWORD'] = 'Password123'

This line will export your DB_PASSWORD in environment. Environment variables are not Python variables.

In pycharm, you have opened python interpreter which parses only python code. It will look for DB_PASSWORD as python variable, not the system environment variable.

Try putting DB_PASSWORD into your command prompt (cmd or shell) , or in python you can list env variables using

print(os.environ)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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