'Python get the original os.environ after it's been modified
With Python, is there a way to get a snapshot of os.environ as it was at the start of the application?
My dilemma is I'm running several python processes using subprocess.run and the environments are inherited. My main application is bundled via PyInstaller, and some runtime-hooks for packages, notably Tcl/Tk, set environment variables that create conflicts for the subprocesses I'm trying to run, which also use Tcl/Tk. I'd like to pass in os.environ as the environment for the subprocess, but strip out anything that was added since the application started. Additionally, since some of these environment variables are being set via PyInstaller's runtime hooks they're occurring before my main application could get a chance to store off a copy of the environment.
Example Code:
import os
from win32api import GetEnvironmentVariable
# This path modification would typically happen via a PyInstaller runtime hook before my
# main application code is run
os.environ["PATH"] = r"C:\test\test\test;" + os.environ["PATH"]
print(os.getenv("PATH").split(';'))
# This was my first attempt, but it picks up the environment changes
win32EnvironmentGrab = GetEnvironmentVariable("PATH").split(';')
print(win32EnvironmentGrab )
assert r"C:\test\test\test" not in win32EnvironmentGrab
# todo: Find a means of getting os.environ without the path modification
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
