I'm on Windows. I am trying to write a Python 2.x script (let's call it setup.py) which would enable the following scenario:
- User runs cmd to open a console window
- In that console window, user runs
setup.py - User finds themselves in the same console window, but now the cmd running there has had its environment (env. variables) modified by
setup.py
setup.py modifies the environment by adding a new environment variable FOO with value foo, and by preneding something to PATH.
On Linux, I would simply use os.exec*e to replace the Python process with a shell with the environment configured.
I tried the same approach on Windows (like os.exec*e(os.environ['ComSpec'])), but it doesn't work, the environment of the newly executed cmd is messed up like this:
Running just
setdoesn't listFOOand doesn't show the effect onPATH. Runningset FOO, however, showsFOO=foo, andecho %FOO%echoesfoo.Running
set PATHorecho %PATH%shows the modifiedPATHvariable. Runningset pathorecho %path%shows the value without the modification (even though env. vars are normally case insensitive on Windows).If I type
exit, the conole remains hanging in some state not accepting input, until I hitCtrl+C. After that, it apparently returns to thecmdwhich originally calledsetup.py.
So clearly, os.exec*e doesn't work for this scenario on Windows. Is there a different way to achieve what I want? Is there a combination of subprocess.Popen() flags which would enable me to exit the calling Python process and leave the called cmd runnig, ideally in the same console? Or would accessing CreateProcess through ctypes help?
If necessary, I would settle for launching a new console window and closing the old one, but I certainly can't afford having the old console window hang in frozen state, waiting for a newly created one to close.