问题描述
我正在尝试在来自 Python 2.4.4 的 .tex 文件上运行 pdflatex.子进程(在 Mac 上):
导入子流程subprocess.Popen(["pdflatex", "fullpathtotexfile"], shell=True)
实际上什么都不做.但是,我可以在终端中运行pdflatex fullpathtotexfile"而没有问题,生成pdf.我错过了什么?
正如其中一个答案中所建议的那样,我尝试过:
return_value = subprocess.call(['pdflatex', '/Users/Benjamin/Desktop/directory/ON.tex'], shell =False)
失败:
回溯(最近一次调用最后一次):文件/Users/Benjamin/Desktop/directory/generate_tex_files_v3.py",第 285 行,在 -toplevel-return_value = subprocess.call(['pdflatex', '/Users/Benjamin/Desktop/directory/ON.tex'], shell =False)文件/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/subprocess.py",第 413 行,调用中返回 Popen(*args, **kwargs).wait()文件/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/subprocess.py",第 543 行,在 __init__ 中错误读取,错误写入)文件/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/subprocess.py",第 975 行,在 _execute_child引发 child_exceptionOSError: [Errno 2] 没有那个文件或目录
该文件确实存在,我可以在终端中运行 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
,call
就够了.
例如:
>>>导入子流程>>>return_value = subprocess.call(['pdflatex', 'textfile'], shell=False) # shell 应该设置为 False如果调用成功,return_value
将被设置为 0,否则为 1.
Popen
的使用通常用于您希望存储输出的情况.例如,您想使用命令 uname
检查内核版本并将其存储在某个变量中:
同样,永远不要设置 shell=True
.
I'm trying to run pdflatex on a .tex file from a Python 2.4.4. subprocess (on a mac):
import subprocess
subprocess.Popen(["pdflatex", "fullpathtotexfile"], shell=True)
which effectively does nothing. However, I can run "pdflatex fullpathtotexfile" in the terminal without issues, generating a pdf. What am I missing?
[EDIT]As suggested in one of the answers, I tried:
return_value = subprocess.call(['pdflatex', '/Users/Benjamin/Desktop/directory/ON.tex'], shell =False)
which fails with:
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
The file does exist and I am able to run pdflatex /Users/Benjamin/Desktop/directory/ON.tex
in the Terminal. Note that pdflatex does throw a good number of warnings... but that shouldn't matter, and this also gives the same error:
return_value = subprocess.call(['pdflatex', '-interaction=batchmode', '/Users/Benjamin/Desktop/directory/ON.tex'], shell =False)
Use the convenience function, subprocess.call
You don't need to use Popen
here, call
should suffice.
For example:
>>> import subprocess
>>> return_value = subprocess.call(['pdflatex', 'textfile'], shell=False) # shell should be set to False
If the call was successful, return_value
will be set to 0, or else 1.
Usage of Popen
is typically for cases when you want the store the output. For example, you want to check for the kernel release using the command uname
and store it in some variable:
>>> process = subprocess.Popen(['uname', '-r'], shell=False, stdout=subprocess.PIPE)
>>> output = process.communicate()[0]
>>> output
'2.6.35-22-generic\n'
Again, never set shell=True
.
这篇关于mac上python子进程中的pdflatex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!