为什么 pdflatex 作为 Python 子进程调用时不起作用?
我正在尝试在 Python 脚本中实现一个函数来自动编译 TeX 文件。我正在尝试使用 子进程模块;这就是我正在做的事情:
def createpdf(output):
args = ['pdflatex', output, '-interaction=nonstopmode']
process = subprocess.call(args,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
stdin = subprocess.PIPE)
当我在终端中使用 TeX 文件运行 pdflatex 时,它可以正常编译。但是当我运行 Python 脚本时,它无法编译。编译过程似乎开始了,但几分钟后,它毫无理由地停止了。我查看了日志文件,它没有打印任何错误消息。
I am trying to implement a function in my Python script to compile a TeX file automatically. I am trying with the subprocess module; this is what I'm doing:
def createpdf(output):
args = ['pdflatex', output, '-interaction=nonstopmode']
process = subprocess.call(args,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
stdin = subprocess.PIPE)
When I run pdflatex with my TeX file in the terminal, it compiles fine. But when I run my Python script, it doesn't compile. It seems like the compile process begins but after a couple of minutes, it stops without any reason. I looked in the log file and it doesn't print any error message.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您将输出管道设置为
subprocess.PIPE
时,子进程会创建一个缓冲区来保存子进程的输出,直到您的进程读取它。如果您从未从process.stdout
和process.stderr
读取数据,pdflatex 可能会填满缓冲区和块。您需要丢弃它们的输出或者只调用
subprocess.call(args)
并让它们流经程序的输出。When you set an output pipe to
subprocess.PIPE
, subprocess creates a buffer to hold the subprocess' output until it's read by your process. If you never read fromprocess.stdout
andprocess.stderr
, pdflatex can fill up the buffer and block.You need to either discard their output or just call
subprocess.call(args)
and let them flow through your program's output.