title: Determine calling executable in Python
body: <p>I'm developing a command-line application that launches a sub-shell with a pre-generated environment.</p>
  
  <pre><code>subprocess.call(subshell, shell=True, env=custom_environment)
  </code></pre>
  
  <p>It launches <code>subshell.bat</code> or <code>subshell.sh</code> depending on which is the current os.</p>
  
  <pre><code>subshell = "subshell.bat" if os.name == "nt" else "subshell.sh"
  </code></pre>
  
  <p>But I'm looking to support Cygwin, Git Bash and others running on Windows, and they each need to be given <code>subshell.sh</code> instead, even though the OS is Windows.</p>
  
  <p>I figured that if there was a way of determining the calling executable of a Python process, I could instead check against that.</p>
  
  <pre><code>if calling_executable in ("bash", "sh"):
    subshell = "subshell.sh"
  elif calling_executable in ("cmd", "powershell"):
    subshell = "subshell.bat"
  else:
    sys.exit()  # unsupported shell
  </code></pre>
  
  <p>But I'm not sure how to do that. Any ideas or suggestions for how to go about it differently?</p>
  
  <p><strong>Usage example</strong></p>
  
  <pre><code>$ mytool --enter
  Entering subshell for bash.exe
  </code></pre>
  
  <hr>
  
  <p>ps. <a href="https://stackoverflow.com/questions/2846416/determine-calling-executable-in-python">this</a> question is phrased exactly the same, but doesn't involve a similar problem, so it didn't feel right to add an unrelated problem to it.</p>