pdflatex在mac上的python子进程中

我正在尝试从Python 2.4.4在.tex文件上运行pdflatex.子进程(在mac上):

import subprocess
subprocess.Popen(["pdflatex", "fullpathtotexfile"], shell=True)

这实际上什么也没做.但是,我可以在终端中运行“pdflatex fullpathtotexfile”而不会出现问题,生成pdf.我错过了什么?

[编辑]
正如其中一个答案所示,我试过:

return_value = subprocess.call(['pdflatex', '/Users/Benjamin/Desktop/directory/ON.tex'], shell =False)

失败的是:

Traceback (most recent call last):
  File "/Users/Benjamin/Desktop/directory/generate_tex_files_v3.py", line 285, in -toplevel-
    return_value = subprocess.call(['pdflatex', '/Users/Benjamin/Desktop/directory/ON.tex'], shell =False)
  File "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/subprocess.py", line 413, in call
    return Popen(*args, **kwargs).wait()
  File "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/subprocess.py", line 543, in __init__
    errread, errwrite)
  File "/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/subprocess.py", line 975, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

该文件确实存在,我可以在终端中运行pdflatex /Users/Benjamin/Desktop/directory/ON.tex.请注意,pdflatex确实会抛出大量警告……但这无关紧要,这也会产生同样的错误:

return_value = subprocess.call(['pdflatex', '-interaction=batchmode', '/Users/Benjamin/Desktop/directory/ON.tex'], shell =False)

解决方法:

使用便利功能,subprocess.call

你不需要在这里使用Popen,打电话就足够了.

例如:

>>> import subprocess
>>> return_value = subprocess.call(['pdflatex', 'textfile'], shell=False) # shell should be set to False

如果调用成功,则return_value将设置为0,否则为1.

Popen的用法通常适用于您希望存储输出的情况.例如,您想使用命令uname检查内核版本并将其存储在某个变量中:

>>> process = subprocess.Popen(['uname', '-r'], shell=False, stdout=subprocess.PIPE)
>>> output = process.communicate()[0]
>>> output
'2.6.35-22-generic\n'

再次,永远不要设置shell = True.

上一篇:c# – 如何从Appharbor应用程序运行外部可执行文件(HTML到PDF生成)?


下一篇:python – 在Matplotlib中使用连字符或减号符号与与Latex的兼容性