Python cannot compile LaTeX file

I am using a Conda Python installation and Jupyter to work with a script producing a LaTeX file I want to compile. These are the relevant code lines:

import subprocess
xelatex= "/usr/local/texlive/2018/bin/x86_64-darwin/xelatex"
subprocess.check_call([xelatex, '-shell-escape', '-file-line-error', '-interaction=nonstopmode', '-synctex=1', my_folder+'/output_file.tex'])


Unfortunately, the LaTeX source file does not get compiled and Python returns a non-zero exit status along the following lines:

---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
<ipython-input-26-fc4edbf90d41> in <module>
1 import subprocess
2 xelatex= "/usr/local/texlive/2018/bin/x86_64-darwin/xelatex"
----> 3 subprocess.check_call([xelatex, '-shell-escape', '-file-line-error', '-interaction=nonstopmode', '-synctex=1', dir_AED+'/registro_lezioni.tex'])

~/anaconda3/lib/python3.6/subprocess.py in check_call(*popenargs, **kwargs)
289         if cmd is None:
290             cmd = popenargs[0]
--> 291         raise CalledProcessError(retcode, cmd)
292     return 0
293

CalledProcessError: Command '['/usr/local/texlive/2018/bin/x86_64-darwin/xelatex', '-shell-escape', '-file-line-error', '-interaction=nonstopmode', '-synctex=1', 'my_folder+'/output_file.tex']' returned non-zero exit status 1.


I have a Tex Live 2018 installation on my Mac that works smoothly with many other apps:

Last login: Sat Nov  3 07:39:44 on ttys000
Restored session: Sat Nov  3 07:33:30 CET 2018MacBook-Pro:~ m*****\$ xelatex
This is XeTeX, Version 3.14159265-2.6-0.99999 (TeX Live 2018) (preloaded format=xelatex) restricted \write18 enabled.
**


Using any other LaTeX-based app, like Texpad, the source file gets compiled with no errors. I went to the Conda terminal window and the problem seems to be that the LaTeX compiler cannot find a ancillary file:

ABD: EveryShipout initializing macros
(/usr/local/texlive/2018/texmf-dist/tex/latex/translator/translator-basic-dicti
onary-English.dict)
(/usr/local/texlive/2018/texmf-dist/tex/latex/siunitx/siunitx-abbreviations.cfg
) (/usr/local/texlive/2018/texmf-dist/tex/latex/wasysym/uwasy.fd)

! LaTeX Error: File fragment.tex' not found.


Nevertheless, the file fragment.tex does live in the same folder, but the LaTeX compiler cannot find it.

In sum, I need to instruct the LaTeX compiler to look for files in the same folder where the main source file is located.

• @Grimler, I can hardly understand why your raised these points since: (1) I wrote that "the file fragment.tex does live in the same folder," (2) I also added that using Texpad "the source file gets compiled with no errors." (3) To state it again: the main source file and the fragment are in the same directory. (4) The problem is totally related to how the Python compiler is being called by Python. – Valerio Nov 3 '18 at 9:02
• Ah, right, you wrote that you got the LaTeX Error: File fragment.tex' not found.-error in the terminal window, not from texpad, my bad – user49901 Nov 3 '18 at 9:08
• tex relative paths are relative to the working directory from where the program was started, not the directory of the main file. can you get python to do cd foo; xeletex file instead of xelatex foo/file ? – David Carlisle Nov 3 '18 at 9:34
• @DavidCarlisle Thanks so much! I have just changed the working directory and the LaTeX file now gets compiled. If I remember correctly, one can also pass the required folder as an additional argument to the subprocess.check_call command, right? – Valerio Nov 3 '18 at 10:05
• @Valerio I can tell you about the tex, not the python:-) – David Carlisle Nov 3 '18 at 10:06

tex relative paths are relative to the working directory from where the program was started, not the directory of the main file. can you get python to do

cd foo; xeletex file


xelatex foo/file


?

So I guess you get the error because you are compiling from the parent of the texfile when using python/conda, but from the texfile directory in texpad.

You could either set the TEXINPUTS environment variable to make xelatex search in custom directories (see \input and absolute paths), or change your include command to the full (or relative path) to fragment.tex.

I don't know how you're including fragment.tex but this means for example changing \input{fragment} to \input{dir_AED/fragment}