I am trying to build a latex file using a subprocess call in a flask web-app.
def pdf(self, ixTestsheet):
build_dir = mkdtemp(prefix="testsheet_")
tsfile = NamedTemporaryFile(dir=build_dir, suffix=".tex", delete=False)
tsfile.write(self.latex(ixTestsheet, _print_type).encode("utf-8"))
old_dir = os.getcwd()
# os.chdir(build_dir)
latexname = os.path.basename(tsfile.name)
tsfile.close()
# added this to check user of subprocess
dirout = subprocess.run(
["whoami.exe"], cwd=build_dir, check=True, capture_output=True
)
current_app.logger.info(dirout)
try:
subprocess.run(
[
current_app.config["LUALATEX_EXE"],
"--interaction=nonstopmode",
latexname,
],
cwd=build_dir,
check=True,
)
# call a second time to get a full build with page numbers etc.
subprocess.run(
[
current_app.config["LUALATEX_EXE"],
"--interaction=nonstopmode",
latexname,
],
cwd=build_dir,
check=True,
)
except subprocess.CalledProcessError as e:
current_app.logger.error(build_dir)
current_app.logger.error(e)
If I run the process from the flask app it fails with the following error. If I run the command from an interactive python session it completes.
[2020-12-04 11:51:13,512] INFO in testsheets: CompletedProcess(args=['whoami.exe'], returncode=0, stdout=b'<DOMAINNAME>\\<USERNAME>\r\n', stderr=b'')
lualatex.exe: No suitable temporary directory found.
Sorry, but "c:/Program Files/MiKTeX 2.9/miktex/bin/x64/lualatex.exe" did not succeed.
FATAL: No suitable temporary directory found.
FATAL: Data:
FATAL: Source: Libraries\MiKTeX\Core\Session\miktex.cpp:79
[2020-12-04 11:51:13,745] ERROR in testsheets: C:\Users\USERNA~1\AppData\Local\Temp\testsheet_oreb8wg5
[2020-12-04 11:51:13,745] ERROR in testsheets: Command '['c:/Program Files/MiKTeX 2.9/miktex/bin/x64/lualatex.exe', '--interaction=nonstopmode', 'tmpfk6un0c7.tex']' returned non-zero exit status 1.
I'm trying to figure out why MiKTeX is not able to find a temporary directory when run from flask, but can when run from an interactive session.
From what I can see:
It seems both TMP
and TEMP
existed in the subprocess
env, however TMP
was set to an empty string. MiKTeX was using TMP
before TEMP
so it failed.
resolved it like this
subprocess.run(
[
current_app.config["LUALATEX_EXE"],
"--interaction=nonstopmode",
latexname,
],
cwd=build_dir,
check=True,
env=dict(os.environ, TMP=os.environ["TEMP"]),
)