简体   繁体   中英

How to set a 'system level' environment variable?

I'm trying to generate an encryption key for a file and then save it for use next time the script runs. I know that's not very secure, but it's just an interim solution for keeping a password out of a git repo.

subprocess.call('export KEY="password"', shell=True) returns 0 and does nothing. Running export KEY="password" manually in my bash prompt works fine on Ubuntu.

subprocess.call('export KEY="password"', shell=True)

creates a shell, sets your KEY and exits: accomplishes nothing.

Environment variables do not propagate to parent process, only to child processes. When you set the variable in your bash prompt, it is effective for all the subprocesses (but not outside the bash prompt, for a quick parallel)

The only way to make it using python would be to set the password using a master python script (using os.putenv("KEY","password") or os.environ["KEY"]="password" ) which calls sub-modules or processes.

This isn't a thing you can do. Your subprocess call creates a subshell and sets the env var there, but doesn't affect the current process, let alone the calling shell.

Using Python:

#SET:
os.environ["EnvVar"] = "1"

#GET:
print os.environ["EnvVar"]

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