作为较大代码的一部分,我试图创建一个使用latexmk
来调用subprocess
编译器的函数,但是我始终得到FileNotFoundError: [Errno 2] No such file or directory: 'latexmk': 'latexmk'
但是,如果我直接在终端中编写命令,则一切正常:latexmk --pdf test.tex
如果很重要,我在MacOS Mojave 10.14.6上,通过anaconda运行python 3.6 spyder
我检查了以下链接:
如果有什么方法可以解决问题,我就错过了。
为了使每个人的生活更轻松,这是.tex文件的链接[您可以使用自己的]: https://drive.google.com/open?id=1DoJnvg2BmbRCzmRmqFYRVybyTQUtyS-h
将type latexmk
放到终端后输出:
latexmk is hashed (/Library/TeX/texbin/latexmk)
这是最小的可复制示例(尽管您的计算机上确实需要Latexmk):
import os, subprocess
def pdf(file_path):
cur_dir = os.getcwd()
dest_dir = os.path.dirname(file_path)
basename = os.path.basename(file_path)
os.chdir(dest_dir)
main_arg = [basename]
command = ["latexmk", "--pdf"] + main_arg
try:
output = subprocess.check_output(command)
except subprocess.CalledProcessError as e:
print(e.output.decode())
raise
os.chdir(cur_dir)
pdf("path to your .tex file")
我感觉自己对子流程的工作方式有严重的误解。有什么想法吗?
更新:如有必要,完整的追溯:
Traceback (most recent call last):
File "<ipython-input-90-341a2810ccbf>", line 1, in <module>
pdf('/Users/sergejczan/Desktop/untitled folder/test.tex')
File "/Users/sergejczan/Desktop/Lab/subprocess error reproduction.py", line 23, in pdf
output = subprocess.check_output(command)
File "/anaconda3/lib/python3.6/subprocess.py", line 336, in check_output
**kwargs).stdout
File "/anaconda3/lib/python3.6/subprocess.py", line 403, in run
with Popen(*popenargs, **kwargs) as process:
File "/anaconda3/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/anaconda3/lib/python3.6/subprocess.py", line 1344, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'latexmk': 'latexmk'
新更新
用我从output = subprocess.check_output(command)
得到的硬编码环境来改变echo $PATH
行,效果很好。
output = subprocess.check_output(command,env = {'PATH': '/anaconda3/bin:/Users/sergejczan/anaconda3/bin:/Users/sergejczan/Desktop/Lab/anaconda2/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin'})
您是否认为有一种方法可以使代码自动找到PATH?