Windows7下Python2.7用subprocess调用py文件未实际执行问题
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.joinexpects separate path components, not a pre-concatenated string like'python' + self.TESTENGINEDIR. This creates a broken path (e.g., ifself.TESTENGINEDIRisC:\test\engine, you'll end up withpythonC:\test\engine\flashimage.py—a path that doesn't exist).- You're trying to run the
.pyfile directly instead of invoking it via the Python interpreter. On Windows, directly executing.pyfiles 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 withflashimage.pyitself, not your subprocess code. - Check working directory: If
flashimage.pyrelies on files in its own folder, addcwd=self.TESTENGINEDIRto yoursubprocess.Popencall to set the correct working directory. - Verify startupinfo: If you need
suinfo, make sure it's not setting flags likeDETACHED_PROCESSorCREATE_NO_WINDOWthat 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

