title: Keyerror in python matching executables
body: <p>I'm getting</p>
  
  <pre><code>Traceback (most recent call last):   File
  "C:\Downloaders\nzbToMedia\TorrentToMedia.py", line 323, in &lt;module&gt;
      state = unpack(dirpath, file, outputDestination)
  File "C:\Downloaders\nzbToMedia\TorrentToMedia.py", line 121, in unpack
      if which(win_7z_exe):   
  File "C:\Downloaders\nzbToMedia\TorrentToMedia.py", line 35, in which
      for path in os.environ["PATH"].split(os.pathsep):   
  File "C:\Python27\lib\os.py", line 423, in __getitem__
      return self.data[key.upper()] 
  KeyError: 'PATH'
  </code></pre>
  
  <p>But I can't seem to figure it out.  This code should work(??)</p>
  
  <pre><code>def which(program):
      import os
      def is_exe(fpath):
          return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
  
      fpath, fname = os.path.split(program)
      if fpath:
          if is_exe(program):
              return program
      else:
          for path in os.environ["PATH"].split(os.pathsep):
              path = path.strip('"')
              exe_file = os.path.join(path, program)
              if is_exe(exe_file):
                  return exe_file
  
      return None
  </code></pre>
  
  <p>Code calling <code>which()</code></p>
  
  <pre><code>if os.name == 'nt':
          win_7z_exes = ['7z.exe', 'C:\\Program Files\\7-Zip\\7z.exe', 'C:\\Program Files (x86)\\7-Zip\\7z.exe',]
          switch_7z = "x -y"
          exts_7z = [".rar", ".zip", ".tar", ".tar.gz", ".tgz", ".tar.bz2", ".tbz", ".tar.lzma", ".tlz", ".tar.xz", ".txz", ".7z", ".xz", ".lzma",]
          for win_7z_exe in win_7z_exes:
              if which(win_7z_exe):
                  EXTRACT_COMMANDS = dict.fromkeys(exts_7z, [win_7z_exe, switch_7z])
                  break
  
      # Using Linux
      elif os.name == 'posix':
          Logger.info("EXTRACTOR: We are using *nix")
          required_cmds=["unrar", "unzip", "tar", "unxz", "unlzma", "7zr"] # Need to add a check for which commands that can be utilized in *nix systems
          EXTRACT_COMMANDS = {
          ".rar": ["unrar", "x -o+ -y"],
          ".zip": ["unzip", ""],
          ".tar.gz": ["tar", "xzf"],
          ".tgz": ["tar", "xzf"],
          ".tar.bz2": ["tar", "xjf"],
          ".tbz": ["tar", "xjf"],
          ".tar.lzma": ["tar", "--lzma xf"],
          ".tlz": ["tar", "--lzma xf"],
          ".txz": ["tar", "--xz xf"],
          ".7z": ["7zr", "x"],
          }
          for cmd in required_cmds:
              if not which(cmd):
                  for k,v in EXTRACT_COMMANDS.items():
                      if cmd in v[0]:
                          Logger.debug("EXTRACTOR: Command %s not found, disabling support for %s", cmd, k)
                          del EXTRACT_COMMANDS[k]
      else:
          Logger.error("EXTRACTOR: Cant determine host OS while extracting, Exiting")
  </code></pre>