1

I'm trying to set an env variable which I want it to be reachable from another windows shell (which is already opened) :

I have tried:

os.environ['start'] = 'test'

Then in a Windows cmd : env.exe | findstr 'start' returns nothing. Variable 'start' does not exist.

Then I tried:

subprocess.call(['setx.exe', 'start', 'test'])
SUCCESS: Specified value was saved

env.exe | findstr 'start' still returns nothing.

How can I get this variable from a windows shell ? Using Windows 10 and python 3. Thanks!

2
  • Are you trying to get access to an environment variable set in a subprocess (hint, you cannot do this - see stackoverflow.com/questions/61755986/…)? Or are you trying to pass environment variables to the subprocess? You question needs clarification. Commented May 12, 2020 at 16:57
  • 1
    setx.exe updates the environment in the registry and broadcasts a WM_SETTINGCHANGE "Environment" message to top-level windows. This includes the Windows shell, Explorer, which reloads its environment from the registry. It does not include console applications such as cmd.exe, which do not create or own any top-level windows. Commented May 12, 2020 at 22:20

1 Answer 1

2

You have to call the Windows SETX from your python code in order to make your environment value saved from your code

os.system("SETX {0} {1} /M".format("start", "test"))

should do the trick. Dont forget to run your script as Administrator. In order for your command prompt to see the changes, make sure you close it and open a new command prompt, then try to find your environment value using env.exe

Sign up to request clarification or add additional context in comments.

4 Comments

Setting a per-user variable with setx.exe would be fine, and not require running as an administrator. But this is all a waste of time since it cannot do what the question wants in terms of updating the environment of an existing instance of the cmd.exe. The only way to do that from outside of the target process would require unreliable code that makes use of undocumented process data structures in order to remotely modify the environment block of another process.
Thanks, but calling os.getenv('start') right after your os.system() command does not show anything (type is None), how do you explain that?
SETX updates the Environment key in Windows, not in the subprocess. If you want to get the Environment key both in subprocess and save it in the windows, you can basically modify code into this: os.environ['start'] = 'test' os.system("SETX {0} {1} /M".format("start", "test")) Or simply restart your subprocess(your python code). See @Eryk Sun's comments
The OP wants the variable to "be reachable from another windows shell (which is already opened)", as in an already running instance of cmd.exe. Even if one can modify the OS environment block of another process, which would be unreliable across different versions of Windows since there's no public API for this, there's still no guarantee that the shell isn't working from a private copy of the environment. That's why I called this a waste of time. OTOH, it could be paired with a batch script to be run in the existing shell that reloads the environment from the registry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.