使用子进程在 Windows 上运行 Python 脚本 [英] Using subprocess to run Python script on Windows

查看:72
本文介绍了使用子进程在 Windows 上运行 Python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法可以在 Windows/Linux/OS X 上运行 Python 脚本?

在后两者上,subprocess.Popen("/the/script.py") 有效,但在 Windows 上我收到以下错误:

回溯(最近一次调用最后一次):文件test_functional.py",第 91 行,在 test_functional 中日志 = tvnamerfiy(tmp)文件test_functional.py",第 49 行,在 tvnamerifiy标准输出 = 管道文件C:\Python26\lib\subprocess.py",第 595 行,在 __init__ 中错误读取,错误写入)文件C:\Python26\lib\subprocess.py",第 804 行,在 _execute_child 中启动信息)WindowsError: [错误 193] %1 不是有效的 Win32 应用程序

<小时><块引用>

monkut 的评论:用例不清楚.为什么要使用子进程来运行 python 脚本?是否存在阻止您导入脚本和调用必要函数的问题?

我正在编写一个快速脚本来测试 Python 命令行工具的整体功能(在各种平台上进行测试).基本上它必须在一个临时文件夹中创建一堆文件,在这个文件夹上运行脚本并检查文件是否正确重命名.

我可以导入脚本并调用该函数,但由于它依赖于 sys.argv 并使用 sys.exit(),所以我需要这样做类似的东西..

导入系统导入电视名称sys.argv.append("-b", "/the/folder")尝试:tvnamer.main()除了 BaseException,errormsg:打印类型(errormsg)

此外,我想捕获 stdout 和 stderr 以进行调试,以防出现问题.

当然更好的方法是以更可单元测试的方式编写脚本,但脚本基本上完成",我正在做最后一批测试,然后再发布1.0"版本(之后我将进行重写/重组,这将更整洁,更易于测试)

基本上,在找到 sys.executable 变量后,将脚本简单地作为进程运行要容易得多.我会把它写成一个 shell 脚本,但这不会是跨平台的.最终脚本可以在这里

找到

解决方案

刚发现 sys.executable - 当前 Python 可执行文件的完整路径,可用于运行脚本(而不是依赖 shbang,这显然不适用于 Windows)

导入系统导入子流程theproc = subprocess.Popen([sys.executable, "myscript.py"])theproc.communicate()

Is there a simple way to run a Python script on Windows/Linux/OS X?

On the latter two, subprocess.Popen("/the/script.py") works, but on Windows I get the following error:

Traceback (most recent call last):
  File "test_functional.py", line 91, in test_functional
    log = tvnamerifiy(tmp)
  File "test_functional.py", line 49, in tvnamerifiy
    stdout = PIPE
  File "C:\Python26\lib\subprocess.py", line 595, in __init__
    errread, errwrite)
  File "C:\Python26\lib\subprocess.py", line 804, in _execute_child
    startupinfo)
WindowsError: [Error 193] %1 is not a valid Win32 application


monkut's comment: The use case isn't clear. Why use subprocess to run a python script? Is there something preventing you from importing the script and calling the necessary function?

I was writing a quick script to test the overall functionality of a Python-command-line tool (to test it on various platforms). Basically it had to create a bunch of files in a temp folder, run the script on this and check the files were renamed correctly.

I could have imported the script and called the function, but since it relies on sys.argv and uses sys.exit(), I would have needed to do something like..

import sys
import tvnamer
sys.argv.append("-b", "/the/folder")
try:
    tvnamer.main()
except BaseException, errormsg:
    print type(errormsg)

Also, I wanted to capture the stdout and stderr for debugging incase something went wrong.

Of course a better way would be to write the script in more unit-testable way, but the script is basically "done" and I'm doing a final batch of testing before doing a "1.0" release (after which I'm going to do a rewrite/restructure, which will be far tidier and more testable)

Basically, it was much easier to simply run the script as a process, after finding the sys.executable variable. I would have written it as a shell-script, but that wouldn't have been cross-platform. The final script can be found here

解决方案

Just found sys.executable - the full path to the current Python executable, which can be used to run the script (instead of relying on the shbang, which obviously doesn't work on Windows)

import sys
import subprocess

theproc = subprocess.Popen([sys.executable, "myscript.py"])
theproc.communicate()

这篇关于使用子进程在 Windows 上运行 Python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆