0

作为较大代码的一部分,我正在尝试使用调用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 吗?

4

0 回答 0