Environment variable is returning 'None'

Question:

I am using windows 11 and the latest version of Pycharm.
I try to save environment variable using pycharm termimal by typing ‘export API_KEY=dfdfdg’
and when I type ‘env’ and run then the termianl show all the environment variable including ‘API_KEY’

but in my code, when I wanna fetch the api key using ‘os.environ.get(‘API_KEY), it returns none.
if I close pycharm and again reopen and type ‘env’ then the "API_KEY" dissapear

When i save my environment variable from my desktop settings then its working fine but I dnt want to save all of the environment variable by going to the settings everytime.

so, what should i do now??

I tried to search on internet as well as chatgpt but did not find the appropriate solution for my problem

Asked By: Ali Sadain Tanvir

||

Answers:

I suggest you use python-dotenv and put all project-related environment variables in .env file:

API_KEY=xxx

Then you can access it using the following code:

from dotenv import load_dotenv

load_dotenv()

# Code of your application, which uses environment variables (e.g. from `os.environ` or 
# `os.getenv`) as if they came from the actual environment.
key = os.environ.get('API_KEY')
Answered By: chenzhongpu

From the first statement I kinda seen or thought you didn’t type the code of export from terminal or this is what chatGPT said to my request to yours. The issue you’re experiencing is due to the nature of environment variables and how they work. When you set an environment variable in a terminal session (like when you use the export command in PyCharm’s terminal), that environment variable is only set for that particular session. When you close the terminal (or PyCharm), that session ends, and any environment variables you set in that session are lost.

If you want an environment variable to persist across sessions, you have a few options:

Set the environment variable in your shell’s startup file. If you’re using a bash shell, you can add the export command to your .bashrc or .bash_profile file. Then, whenever you start a new terminal session, the shell will automatically set the environment variable. The exact file (.bashrc vs .bash_profile) can depend on your specific system configuration.

Set the environment variable in PyCharm’s configuration. PyCharm lets you specify environment variables for run/debug configurations. To do this, go to "Run" -> "Edit Configurations", select the configuration for your project, and then add the environment variable under the "Environment variables" section.

Set the environment variable system-wide. This process can depend on your operating system. On Windows, you can set system-wide environment variables through the System Properties -> Advanced -> Environment Variables menu. These environment variables will be available to all processes, not just your terminal sessions or PyCharm.

Remember, saving sensitive information like API keys in your code or version control systems is not a good practice. Using environment variables is a better approach as it separates the sensitive information from the actual code.

Also, if you’re working on a shared or production system, be aware that system-wide environment variables can be read by all processes, which might be a security risk depending on the sensitivity of your data.

Yet if that’s not helpful or don’t have gpt-4 please show responce of this above and I will get windows 11 just to fix this problem.