Python Forum
os.environ not setting current shell variables in Windows?
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
os.environ not setting current shell variables in Windows?
#1
Hi,
Having some trouble with a project. Using Python 2.7 and 3.5 (trying to keep things compatible with both for simplicity). Using 3.5 for most of my first-pass testing/dev.

I'm trying to set an environment variable in the user's profile on Windows. Currently running Win 10, but have seen the same behavior on 8.1.

I can set the environment variable via registry key (winreg.SetValueEx) and send the system broadcast needed to have future processes pick up the change. But what I'm trying to do now is set the environment for the current process so I can spawn a child that will inherit the new setting.
os.environ["FOO"] = value
doesn't seem to be setting the current process' environment properly, running "set" after the script completes doesn't show the variable set in the current process. I can't seem to find much beyond "it should work". Is there another, more effective, way to set the environment variables for the current process? My reading of putenv() is that it wouldn't work to set the value here, is that correct?

Any help is appreciated, and thank you.
Brian
Reply
#2
Using os.environ: http://stackoverflow.com/questions/59716...-variables
Reply
#3
Thanks, I'll look through those responses again to see if I missed anything, I suspect I'm going to have to resort to spawning multiple cmd processes to try and force the inheritance, as the straight use of os.environ is not setting the current environment.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to find out from outside Python (in Windows) the current version of Python? pstein 4 742 Oct-04-2023, 10:01 AM
Last Post: snippsat
  batch file for running python scipt in Windows shell MaartenRo 2 1,895 Jan-21-2022, 02:36 PM
Last Post: MaartenRo
  Setting permanent user variables in Windows coder420 2 1,422 Jan-04-2022, 10:42 AM
Last Post: Larz60+
  Key Error: os.environ["HOME"] Anldra12 12 16,449 Jun-18-2021, 04:11 PM
Last Post: Axel_Erfurt
  Help Setting Multiple Variables bzowk 0 1,605 Jul-18-2020, 06:59 PM
Last Post: bzowk
  How to use subprocess send commands to windows shell hlhp 3 4,446 Nov-26-2019, 04:40 AM
Last Post: LeanbridgeTech
  Setting DCHP in Windows 10 with Python Martin2998 4 3,637 Dec-07-2018, 10:15 PM
Last Post: Martin2998
  setting pythonpath variable in environment variables saisankalpj 3 3,474 Dec-05-2018, 10:33 PM
Last Post: Gribouillis
  Looping through variables and setting them ColdDeath 7 3,993 Dec-17-2017, 02:48 PM
Last Post: ColdDeath
  SOLVED: best way to block (wait on) shell calls to multiple windows programs at once? ezdev 0 2,605 Dec-10-2017, 06:42 AM
Last Post: ezdev

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020