<legend id='SF41X'><style id='SF41X'><dir id='SF41X'><q id='SF41X'></q></dir></style></legend>

        <small id='SF41X'></small><noframes id='SF41X'>

        • <bdo id='SF41X'></bdo><ul id='SF41X'></ul>
        <tfoot id='SF41X'></tfoot>
      1. <i id='SF41X'><tr id='SF41X'><dt id='SF41X'><q id='SF41X'><span id='SF41X'><b id='SF41X'><form id='SF41X'><ins id='SF41X'></ins><ul id='SF41X'></ul><sub id='SF41X'></sub></form><legend id='SF41X'></legend><bdo id='SF41X'><pre id='SF41X'><center id='SF41X'></center></pre></bdo></b><th id='SF41X'></th></span></q></dt></tr></i><div id='SF41X'><tfoot id='SF41X'></tfoot><dl id='SF41X'><fieldset id='SF41X'></fieldset></dl></div>

      2. 使子进程 find git 在 Windows 上可执行

        时间:2023-07-21
        <tfoot id='nDb8J'></tfoot>
          1. <small id='nDb8J'></small><noframes id='nDb8J'>

              <tbody id='nDb8J'></tbody>

                <bdo id='nDb8J'></bdo><ul id='nDb8J'></ul>
                  <legend id='nDb8J'><style id='nDb8J'><dir id='nDb8J'><q id='nDb8J'></q></dir></style></legend>

                • <i id='nDb8J'><tr id='nDb8J'><dt id='nDb8J'><q id='nDb8J'><span id='nDb8J'><b id='nDb8J'><form id='nDb8J'><ins id='nDb8J'></ins><ul id='nDb8J'></ul><sub id='nDb8J'></sub></form><legend id='nDb8J'></legend><bdo id='nDb8J'><pre id='nDb8J'><center id='nDb8J'></center></pre></bdo></b><th id='nDb8J'></th></span></q></dt></tr></i><div id='nDb8J'><tfoot id='nDb8J'></tfoot><dl id='nDb8J'><fieldset id='nDb8J'></fieldset></dl></div>
                • 本文介绍了使子进程 find git 在 Windows 上可执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  import subprocess
                  
                  proc = subprocess.Popen('git status')
                  print 'result: ', proc.communicate()
                  

                  我的系统路径中有 git,但是当我像这样运行子进程时,我得到:
                  WindowsError: [错误2]系统找不到指定的文件

                  I have git in my system path, but when I run subprocess like this I get:
                  WindowsError: [Error 2] The system cannot find the file specified

                  如何让子进程在系统路径中找到 git?

                  How can I get subprocess to find git in the system path?

                  Windows XP 上的 Python 2.6.

                  Python 2.6 on Windows XP.

                  推荐答案

                  这里看到的问题是 Windows API 函数 CreateProcess,由子进程在底层使用,不会自动解析除 .exe.在 Windows 上,'git' 命令实际上是作为 git.cmd 安装的.因此,您应该修改您的示例以显式调用 git.cmd:

                  The problem you see here is that the Windows API function CreateProcess, used by subprocess under the hood, doesn't auto-resolve other executable extensions than .exe. On Windows, the 'git' command is really installed as git.cmd. Therefore, you should modify your example to explicitly invoke git.cmd:

                  import subprocess
                  
                  proc = subprocess.Popen('git.cmd status')
                  print 'result: ', proc.communicate()
                  

                  gitshell==True 时起作用的原因是 Windows shell 自动将 git 解析为 git.cmd.

                  The reason git works when shell==True is that the Windows shell auto-resolves git to git.cmd.

                  import subprocess
                  import os.path
                  
                  def resolve_path(executable):
                      if os.path.sep in executable:
                          raise ValueError("Invalid filename: %s" % executable)
                  
                      path = os.environ.get("PATH", "").split(os.pathsep)
                      # PATHEXT tells us which extensions an executable may have
                      path_exts = os.environ.get("PATHEXT", ".exe;.bat;.cmd").split(";")
                      has_ext = os.path.splitext(executable)[1] in path_exts
                      if not has_ext:
                          exts = path_exts
                      else:
                          # Don't try to append any extensions
                          exts = [""]
                  
                      for d in path:
                          try:
                              for ext in exts:
                                  exepath = os.path.join(d, executable + ext)
                                  if os.access(exepath, os.X_OK):
                                      return exepath
                          except OSError:
                              pass
                  
                      return None
                  
                  git = resolve_path("git")
                  proc = subprocess.Popen('{0} status'.format(git))
                  print 'result: ', proc.communicate()
                  

                  这篇关于使子进程 find git 在 Windows 上可执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <small id='czFW4'></small><noframes id='czFW4'>

                    <tbody id='czFW4'></tbody>
                  1. <legend id='czFW4'><style id='czFW4'><dir id='czFW4'><q id='czFW4'></q></dir></style></legend>
                    <i id='czFW4'><tr id='czFW4'><dt id='czFW4'><q id='czFW4'><span id='czFW4'><b id='czFW4'><form id='czFW4'><ins id='czFW4'></ins><ul id='czFW4'></ul><sub id='czFW4'></sub></form><legend id='czFW4'></legend><bdo id='czFW4'><pre id='czFW4'><center id='czFW4'></center></pre></bdo></b><th id='czFW4'></th></span></q></dt></tr></i><div id='czFW4'><tfoot id='czFW4'></tfoot><dl id='czFW4'><fieldset id='czFW4'></fieldset></dl></div>

                      <tfoot id='czFW4'></tfoot>

                          • <bdo id='czFW4'></bdo><ul id='czFW4'></ul>