Alejandro
Alejandro

Reputation: 5236

Why doesn't pdflatex work when called as a Python subprocess?

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.

Upvotes: 1

Views: 2228

Answers (1)

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 from process.stdout and process.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.

Upvotes: 1

Related Questions