title: How to read out *new* OS environment variables in Python?
body: <p>Reading out OS environment variables in Python is a piece of cake. Try out the following three code lines to read out the <code>'PATH'</code> variable in Windows:</p>
  
  <pre><code>    #################
      #   PYTHON 3.5  #
      #################
      &gt;&gt; import os
      &gt;&gt; pathVar = os.getenv('Path')
      &gt;&gt; print(pathVar)
  
          C:\Anaconda3\Library\bin;C:\Anaconda3\Library\bin;
          C:\WINDOWS\system32;C:\Anaconda3\Scripts;C:\Apps\SysGCC\arm-eabi\bin;
          ...
  </code></pre>
  
  <p>Now make a small change in the <code>'PATH'</code> variable. You can find them in:</p>
  
  <blockquote>
    <p>Control Panel >> System and Security >> System >> Advanced system settings >>
    Environment variables</p>
  </blockquote>
  
  <p>If you run the same code in Python, the change is not visible! You've got to close down the terminal in which you started the Python session. When you restart it, and run the code again, the change will be visible.</p>
  
  <p>Is there a way to see the change immediately - without the need to close python? This is important for the application that I'm building.</p>
  
  <p>Thank you so much :-)</p>
  
  <hr>
  
  <p><strong>EDIT :</strong></p>
  
  <p>Martijn Pieters showed me the following link:
  <a href="https://stackoverflow.com/questions/171588/is-there-a-command-to-refresh-environment-variables-from-the-command-prompt-in-w">Is there a command to refresh environment variables from the command prompt in Windows?</a></p>
  
  <p>There are many options mentioned in that link. I chose the following one (because it is just a batch file, no extra dependencies):</p>
  
  <pre><code>            REM   --------------------------------------------
              REM   |            refreshEnv.bat                |
              REM   --------------------------------------------
      @ECHO OFF
      REM Source found on https://github.com/DieterDePaepe/windows-scripts
      REM Please share any improvements made!
  
      REM Code inspired by https://stackoverflow.com/questions/171588/is-there-a-command-to-refresh-environment-variables-from-the-command-prompt-in-w
  
      IF [%1]==[/?] GOTO :help
      IF [%1]==[/help] GOTO :help
      IF [%1]==[--help] GOTO :help
      IF [%1]==[] GOTO :main
  
      ECHO Unknown command: %1
      EXIT /b 1 
  
      :help
      ECHO Refresh the environment variables in the console.
      ECHO.
      ECHO   refreshEnv       Refresh all environment variables.
      ECHO   refreshEnv /?        Display this help.
      GOTO :EOF
  
      :main
      REM Because the environment variables may refer to other variables, we need a 2-step approach.
      REM One option is to use delayed variable evaluation, but this forces use of SETLOCAL and
      REM may pose problems for files with an '!' in the name.
      REM The option used here is to create a temporary batch file that will define all the variables.
  
      REM Check to make sure we dont overwrite an actual file.
      IF EXIST %TEMP%\__refreshEnvironment.bat (
        ECHO Environment refresh failed!
        ECHO.
        ECHO This script uses a temporary file "%TEMP%\__refreshEnvironment.bat", which already exists. The script was aborted in order to prevent accidental data loss. Delete this file to enable this script.
        EXIT /b 1
      )
  
      REM Read the system environment variables from the registry.
      FOR /F "usebackq tokens=1,2,* skip=2" %%I IN (`REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"`) DO (
        REM /I -&gt; ignore casing, since PATH may also be called Path
        IF /I NOT [%%I]==[PATH] (
          ECHO SET %%I=%%K&gt;&gt;%TEMP%\__refreshEnvironment.bat
        )
      )
  
      REM Read the user environment variables from the registry.
      FOR /F "usebackq tokens=1,2,* skip=2" %%I IN (`REG QUERY HKCU\Environment`) DO (
        REM /I -&gt; ignore casing, since PATH may also be called Path
        IF /I NOT [%%I]==[PATH] (
          ECHO SET %%I=%%K&gt;&gt;%TEMP%\__refreshEnvironment.bat
        )
      )
  
      REM PATH is a special variable: it is automatically merged based on the values in the
      REM system and user variables.
      REM Read the PATH variable from the system and user environment variables.
      FOR /F "usebackq tokens=1,2,* skip=2" %%I IN (`REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH`) DO (
        ECHO SET PATH=%%K&gt;&gt;%TEMP%\__refreshEnvironment.bat
      )
      FOR /F "usebackq tokens=1,2,* skip=2" %%I IN (`REG QUERY HKCU\Environment /v PATH`) DO (
        ECHO SET PATH=%%PATH%%;%%K&gt;&gt;%TEMP%\__refreshEnvironment.bat
      )
  
      REM Load the variable definitions from our temporary file.
      CALL %TEMP%\__refreshEnvironment.bat
  
      REM Clean up after ourselves.
      DEL /Q %TEMP%\__refreshEnvironment.bat
  
      ECHO Environment successfully refreshed.
  </code></pre>
  
  <p>This solution works on my computer (64-bit Windows 10). The environment variables get updated. However, I get the following error:</p>
  
  <pre><code>    ERROR: The system was unable to find the specified registry key or value.
  </code></pre>
  
  <p>Strange.. I get an error, but the variables do get updated.</p>
  
  <hr>
  
  <p><strong>EDIT :</strong></p>
  
  <p>The environment variables get updated when I call the batch file <code>'refreshEnv'</code> directly in the terminal window. But it doesn't work when I call it from my Python program:</p>
  
  <pre><code>    #################################
      #          PYTHON 3.5           #
      #   -------------------------   #
      # Refresh Environment variables #
      #################################
      def refresh(self):
          p = Popen(self.homeCntr.myState.getProjectFolder() + "\\refreshEnv.bat", cwd=r"{0}".format(str(self.homeCntr.myState.getProjectFolder())))
      ###
  </code></pre>
  
  <p>Why? Is it because Popen runs the batch file in another cmd terminal, such that it doesn't affect the current Python process?</p>