0

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?

Community
  • 1
  • 1
XJiang
  • 13
  • 5
  • 4
    Is `latexmk` an alias or shell function in your shell configuration, rather than another command? If not, is your Python script running with the same `PATH` as your interactive shell? – chepner Nov 18 '19 at 20:15
  • Sort of the same question that @chepner asked: what is the output of `which latexmk`? – larsks Nov 18 '19 at 20:16
  • Don't add a traceback in comments...just update your question. This holds true for pretty much any information that people ask for. – larsks Nov 18 '19 at 20:17
  • You would have to dumb it down quite a bit, I started coding 4 months ago, how would you know if the ```latexmk``` is an alias? – XJiang Nov 18 '19 at 20:18
  • Updated the question again, with bold, is that useful/does that answer the question? – XJiang Nov 18 '19 at 20:35
  • Instead of launching latexmk, with subprocess,, try launching `echo $PATH` and compare it with what you get in the terminal, also it's possible that in terminal you'd be in a bash shell while subprocess might run things with sh. Check on how to pass the environment with subprocess and/or try running latexmk with the full path – Lohmar ASHAR Nov 18 '19 at 20:49
  • `import os; os.environ['PATH']` is one way to examine, from within Python, the path that `subprocess` will search in order to find `latexmk`—if it's not found in one of those directories, it won't be launched. One guess is that the result from inside Python is different from what you get from `echo $PATH` at your shell prompt—if so, reasons for that might be (a) spyder hasn't been restarted since `latexmk` was installed; or (b) your shell adds to `$PATH` as part of its user-specific startup, in `.bash_profile` (or whatever the equivalent is for your shell) whereas spyder does not. – jez Nov 18 '19 at 21:13
  • Ok, there is a discrepancy in the PATH: terminal: ```echo $PATH``` gives ```:/anaconda3/bin:/Users/sergejczan/anaconda3/bin:/Users/sergejczan/Desktop/Lab/anaconda2/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin ``` While python ```os.environ['PATH']``` gives ```'/anaconda3/bin:/anaconda3/condabin:/usr/bin:/bin:/usr/sbin:/sbin'``` I think I could hardcode the problem for personal use, but any pointers in how to fix it for any computer? Even a suggestion for a google search will be nice – XJiang Nov 18 '19 at 23:27
  • Seems that inside your context, the PATH is altered/different from whatever is you "standard" PATH, this means that you have to look on how the PATH is managed in the anaconda context and make it inherit the system value(s). For a quick&dirty fix, in your code, you could add `/Library/TeX/texbin` to the PATH, `os.environ['PATH']+=":/Library/TeX/texbin"` before calling subprocess. – Lohmar ASHAR Nov 19 '19 at 13:07

0 Answers0