Subprocess No such file or directory error

1.1k Views Asked by At

As part of larger code, I am trying to make a function that calls to a latexmk compiler using subprocess, but I consistently get FileNotFoundError: [Errno 2] No such file or directory: 'latexmk': 'latexmk'

However, If I write the command directly in the terminal, everything works: latexmk --pdf test.tex

In case it is important, I am on MacOS Mojave 10.14.6, running python 3.6 spyder through anaconda

I have checked the following links:

If anything there solves the problem, I missed it.

To make everyone's life easier, here's a link to a .tex file [you can use your own]: https://drive.google.com/open?id=1DoJnvg2BmbRCzmRmqFYRVybyTQUtyS-h

Afer putting type latexmk to terminal it outputs:

latexmk is hashed (/Library/TeX/texbin/latexmk)

Here is the minimal reproducible example (you do need latexmk on your computer though):


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

I have a feeling that I am grossly misunderstanding the way subprocess works. Any ideas?

Update: In case neccessary, the full traceback:

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'

New Update

Changing the output = subprocess.check_output(command) line with the hardcoded envirnoment that I got from echo $PATH worked wonderfully.

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'})

Would you think that there is a way to make the code find the PATH automatically?

0

There are 0 best solutions below