繁体   English   中英

pdflatex在mac上的python子进程中

[英]pdflatex in a python subprocess on mac

我正在尝试从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

你不需要在这里使用Popencall就足够了。

例如:

>>> 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

你可能想要:

output = Popen(["pdflatex", "fullpathtotexfile"], stdout=PIPE).communicate()[0]
print output

要么

p = subprocess.Popen(["pdflatex" + " fullpathtotexfile"], shell=True)
sts = os.waitpid(p.pid, 0)[1]

(从这个子流程文档页面部分无耻地撕掉)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM