You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows7下Python2.7用subprocess调用py文件未实际执行问题

Troubleshooting: subprocess returns 'Success' but flashimage.py doesn't run (Windows 7 + Python 2.7)

Let's break down why your flashimage.py isn't actually executing even though subprocess reports success—this is almost certainly tied to how you're constructing the command and invoking the script. Here's how to fix it step by step:

1. Your command path is incorrectly constructed

Looking at your program method, this line is a critical mistake:

self.blueflashcmd = os.path.join('python' + self.TESTENGINEDIR, 'flashimage.py')
  • os.path.join expects separate path components, not a pre-concatenated string like 'python' + self.TESTENGINEDIR. This creates a broken path (e.g., if self.TESTENGINEDIR is C:\test\engine, you'll end up with pythonC:\test\engine\flashimage.py—a path that doesn't exist).
  • You're trying to run the .py file directly instead of invoking it via the Python interpreter. On Windows, directly executing .py files depends on system file associations, which are unreliable in subprocess contexts (especially if multiple Python versions are installed).

Fix the command arguments

Rewrite your program method to explicitly call Python 2.7 and use the correct script path:

def program(self):
    # Use the full path to Python 2.7 exe if it's not in your system PATH
    python_exe = "python.exe"  # Or "C:\Python27\python.exe"
    # Correctly build the path to flashimage.py
    flashimage_path = os.path.join(self.TESTENGINEDIR, "flashimage.py")
    
    # Verify the script exists before trying to run it
    if not os.path.exists(flashimage_path):
        raise ProgramError(f"Flashimage script not found at: {flashimage_path}")
    
    # Build args as [python interpreter, script path]
    args = [python_exe, flashimage_path]
    output = self.my_check_output(args)
    
    if 'Success' not in output:
        error_msg = output.strip().split('\n')[-1]
        raise ProgramError(error_msg)

2. Your output reading logic is risky

The while p.poll() is None loop with time.sleep(0.1) can lead to unread output or deadlocks, especially if flashimage.py produces more output than fits in the buffer. Python's subprocess.Popen.communicate() is the recommended way to read stdout/stderr safely.

Update my_check_output for reliable output reading

def my_check_output(self, args, retry=False):
    start = time.clock()
    # Temporarily remove startupinfo if you're unsure it's configured correctly
    # Add it back once you confirm the script runs without it
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    
    # Use communicate() to safely read all output and wait for the process to finish
    output, _ = p.communicate()
    # Convert byte string to regular string (Python 2.7 specific)
    output = output.strip() if isinstance(output, str) else output.decode('utf-8').strip()
    
    if p.returncode != 0:
        raise ProgramError("RETURN %d: %s" % (p.returncode, output))
    
    if not output:
        if not retry:
            return self.my_check_output(args, retry=True)
        else:
            raise ProgramError('ERROR: No output received from the command')
    
    return output

3. Additional checks to rule out other issues

  • Test the command manually: Open a cmd prompt and run the exact command you're building (e.g., python C:\path\to\flashimage.py). If it doesn't run here, the problem is with flashimage.py itself, not your subprocess code.
  • Check working directory: If flashimage.py relies on files in its own folder, add cwd=self.TESTENGINEDIR to your subprocess.Popen call to set the correct working directory.
  • Verify startupinfo: If you need suinfo, make sure it's not setting flags like DETACHED_PROCESS or CREATE_NO_WINDOW that might prevent the script from executing properly. Try removing it temporarily to test.

These changes should ensure that flashimage.py actually runs and you get accurate output from it.

内容的提问来源于stack exchange,提问作者LoadLibrary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:43:40