Environment variables don't last after being set (in a container) - python-3.x

I don't know if being inside a docker container has any relation with the problem but for the record I am running all inside a container.
I tried running this script
import os
os.environ['A_VAR']='aValue'
thevalue=os.environ.get('A_VAR',None)
print(thevalue)
with this I set the environment A_VAR to some value and I can see it from the print that is set
then I run the following
import os
'
thevalue=os.environ.get('A_VAR',None)
print(thevalue)
and no, the value is not set.
Running ``printenv` also shows that the values is not set.
Why is setting the environment variable not working and how it should be done?

Environment variables are local the the process setting it and all processes spawned from it (they inherit the environment). So you can set the environment for the child processes, but not for the parent.
Your python script runs as its own process so any changes it made to the environment disappear when this process exits.

Related

Windows Environment Variables - Troubling accessing updated environment variables in program

I have wrote an initialization script that sets user environment variables which are keys that have been hashed and encrypted...Once the keys have been created the key encryption exe is no longer required. I want to launch the main application and remove the init file containing the hashing and key encryption functions.
I am not having any trouble with any of the above...Everything works as should when independent of each other. The problem is that in order for the main application to have access to the newly created environment variables I need the init script to completely exit...
Everything I have tried, Popen with flags, os.system() and others have still left me in a situation where the parent process ends and the main application launches, however, the environment variables have not updated...I close and relaunch main.py and...boom the program sees the updated variables and all is fine.
All I want is the init script to run, spawn a new process that is not linked at all with init.py and then exit so it can be removed. I thought this would be simple but after many hours of head scratching and trying numerous things, I am still no closer.
If I have to I will simply bundle it as two separate .exe files but I wanted it to be a one click install type thing.
I am running windows 10 and this can be platform specific.
Links looked at:
How to stop/terminate a python script from running?
Using a Python subprocess call to invoke a Python script
Starting a separate process
https://docs.python.org/2/library/subprocess.html
Python: Howto launch a full process not a child process and retrieve the PID
And more...
Current closest result
p = Popen(["python","UserInterface.py"], stdin=PIPE, stdout=PIPE, stderr=PIPE,
creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP)
Create an environment block, set the environment variable using SetEnvironmentVariable, and use CreateProcess to specify this environment block for the created process.
MSDN DOC:
To specify a different environment for a process, create a new
environment block and pass the pointer to it as a parameter to the
CreateProcess function.
...
To programmatically add or modify system environment variables, add
them to the
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session
Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE
message with lParam set to the string "Environment". This allows
applications, such as the shell, to pick up your updates.

Set environment variable with shelljs

I'm trying to set an env variable in node with shelljs
so here's the psudo code:
const shell = require('shelljs');
shell.exec('export MM=2');
shell.exec('echo $MM');
but this does not printout the value of MM
Any suggestions on how to set an env variable through node with export (execute bash command)?
Are you familiar with how environment variables work in general? The wikipedia article has a good high level summary here: https://en.wikipedia.org/wiki/Environment_variable
One of the more unique things about environment variables is how they behave across process boundaries. Each process has its own set of environment variables. You can modify the environment variables in your own process without any issues. Whenever you spawn (fork + exec) a child process, it inherits your set of environment variables. If you are the child process (the process that got execed), you can not set the environment variable of your parent process.
You might realize now that if process A creates a child process B, and B modifies the environment variables, A will not see the changes.
So shells handle this specially. export is a shell-built in. In other words, bash (or any other shell) will not actually execute an export command by invoking a binary. Instead the shell will understand what export needs to do and do that directly, adjusting the environment variables in the shell process, not in a separate child process. Then any further command that gets run will inherit the (updated) environment variables from the shell.
You need to do the same.
shelljs provides a separate object, env, for this purpose:
shell.env["MM"] = "2";

Passing and updating an environment variable constantly

I need a way to constantly update a environment variable from a script. I need to use that environment variable in another program almost real time.
What I have is this code:
# !bin/bash
while :
do
AMA="$(cut -c 1-13 text.txt)"
source directory/this_script
done
I am getting the right information from my file when running cut this way. I just need to get this variable permanently updating if possible. Also a way to even get it to the environment.
Slackware Linux

Change environment variable value during execution [duplicate]

This question already has answers here:
Is there a way to change the environment variables of another process in Unix?
(12 answers)
Closed 9 years ago.
Consider the following Ruby code
sleep 10
puts "Foo is #{ENV['foo']}"
Saving this file to envtest.rb
Running this from the shell:
export foo=bar
ruby envtest.rb &
export foo=baz
( ... 10 seconds later ... )
=> Foo is bar
It appears that the environment is evaluated when the ruby interpreter is launched. Is it possible to update environment variables during execution and have those changes reflected in running processes? If so, how?
You can change the value during runtime - from inside the ruby script - using:
ENV['VARIABLE_NAME'] = 'value'
There is no option to change environment values from outside the process after it has been started. That's by design, as the environment will be passed at process startup.
No. This is not possible. One process can never directly manipulate the environment of a different already-running process. All you can ever do is set the environment on unborn children, then create them.
The only other approach is via active, negotiated communication back to the parent. That’s why the output from tset(1) (that is, of tset -s) is always evaluated by the parent.

DejaGNU - want to use environment variable from Linux throughout tests

I have some existing DejaGNU tests I need to modify, I want to replace some hard coded /dev entries with an environment variable, e.g. instead of /dev/ttyS0 I want /dev/$PORT where PORT is defined in the parent Linux shell.
How do I get a variable from the parent shell into DejaGNU?
$env(PORT)
If you want to test whether the environment variable is set, you can use
[info exists env(PORT)]

Resources