Tomergt45
Tomergt45

Reputation: 679

Can you set or delete an environment variable in python globally in windows?

I need to have an option to add and remove global environment variables on runtime from my python code, is it possible?

Upvotes: 1

Views: 4386

Answers (1)

Meny Issakov
Meny Issakov

Reputation: 1421

You can set it in the very first beginning of your program (top of the main file your execute):

import os
os.environ['ENV_VARIABLE_NAME'] = 'VALUE'

but a better practice would be setting it outside of your application, e.g. if you have something running your app, then first use it to set the env variables, and then run your app.

even if it's shell: export VAR=VALUE;python myapp.py...

Upvotes: 3

Related Questions