'Solving "500: Internal Server Error, nbconvert failed: xelatex not found in PATH"

Whenever I try to export a Jupyter notebook as a PDF I get the following error in a separate window:

500 : Internal Server Error
The error was:

nbconvert failed: xelatex not found on PATH, if you have not installed xelatex you may need to do so. Find further instructions at https://nbconvert.readthedocs.io/en/latest/install.html#installing-tex.

I am running macOS Sierra 10.12.6.

Things I have tried:

  • Re-installing nbconvert through pip (in the Jupyter notebook) and conda (terminal)
  • Installing xelatex for Mac
  • Re-installing Mactex
  • Installing pandoc (both through pip and through "conda install -c conda-forge pandoc")

Running

!echo $PATH

Yields:

/Users/ed/anaconda/bin:/Users/ed/anaconda/bin:/usr/bin:/bin:/usr/sbin:/sbin

I located the directory containing xelatex as per @einsweniger suggestion here: /usr/local/texlive/bin/x86_64-darwin. I copied and pasted XeLatex into the bin directory above, and get a new error:

```nbconvert failed: PDF creating failed, captured latex output:
warning: kpathsea: configuration file texmf.cnf not found in these directories: /Users/e/anaconda/bin:/Users/ed/anaconda/bin/share/texmf-local/web2c:/Users/ed/anaconda/bin/share/texmf-dist/web2c:/Users/ed/anaconda/bin/share/texmf/web2c:/Users/ed/anaconda/bin/texmf-local/web2c:/Users/ed/anaconda/bin/texmf-dist/web2c:/Users/ed/anaconda/bin/texmf/web2c:/Users/ed/anaconda:/Users/edefilippis/anaconda/share/texmf-local/web2c:/Users/e/anaconda/share/texmf-dist/web2c:/Users/ed/anaconda/share/texmf/web2c:/Users/ed/anaconda/texmf-
This is XeTeX, Version 3.14159265-2.6-0.99999 (TeX Live 2018) (preloaded format=xelatex)

kpathsea: Running mktexfmt xelatex.fmt
I can't find the format file `xelatex.fmt'!
```

I also put xelatex.fmt in the directory, but am still getting the error.



Solution 1:[1]

I encountered the same issue. As people previously stated, the problem lies in that xlatex isn't found in your PATH environment variable.

A solution that worked for me was to run the following from the notebook:

!export PATH=/Library/TeX/texbin:$PATH

Or in a command line simply:

export PATH=/Library/TeX/texbin:$PATH

And then run the export to pdf from a command line (within your virtual environment if there is one in place) as follows:

 jupyter nbconvert your_notebook.ipynb --to pdf

This should create a pdf of your notebook on the same directory it is running.

Solution 2:[2]

I'm not accustomed with Mac install paths, from what the basictex faq says, I gather the executeables should live in /usr/local/texlive/bin/x86_64-darwin Running which xelatex in a terminal might also help finding where the command is, but that only works if the directory is already in your PATH variable.

Another method would be running find / -name 'xelatex' if it is not within your PATH so you might find it that way (might take a while as this will search you whole harddrive).

Once you've found it and added the path to your PATH variable, you should also check if the jupyter has the correct PATH by running

import os
print(os.environ['PATH'])

within a notebook.

Solution 3:[3]

$ sudo apt-get install texlive-xetex texlive-fonts-recommended texlive-plain-generic

Solution 4:[4]

I've run into similar issues in the past with paths in python when using tensorflow in Windows and Linux. As is pointed out by others, using PATH is the way to go (i.e update in .bashrc if it was linux), but I've found the code below to resolve path issues on a script by script basis.

import sys
if "\your\path\to\xelatex" not in sys.path:
    print('adding path') # I just add this to know if the path was present or not.
    sys.path.append("\your\path\to\xelatex")

essentially would check PATH for what it is you are looking for and then adds it if it is missing. You probably don't even need the 'if' statement.
Perhaps not the most practical way has worked well for me where I know the path in question is perhaps a 'rarely used' or 'one time use' PATH add...

So as was pointed out in comments above (and I think you already have) find the location of the file and add its location using the above code.

Some more reading on it: https://docs.python.org/3/tutorial/modules.html 6.2 Standard Modules

Solution 5:[5]

On mac, you can install mactex using homebrew with the following: brew install --cask mactex

Solution 6:[6]

If (You are using jupyter the easier way I found was to)

  • Download the file in latex.
  • Upload it to overleaf.
  • And then download it as a pdf

Else if ( You are using google collab)

  • Download as ipynb
  • Upload to jupyter and repeat the above steps.

Solution 7:[7]

If there is an update available maybe try and update Jupyter

Solution 8:[8]

Add "env": {"PATH":"$PATH"} to your kernel.json, for me that was in /opt/anaconda3/share/jupyter/kernels/python3/kernel.json.

JupyterLab or Anaconda seems to override the system PATH by default, if you set this then the PATH will be the same in JupyterLab and in your shell, so if you have anything custom installed like xelatex, it will show up in JupyterLab.

Solution 9:[9]

Run this command in your jupyter terminal (or in your environment if you have any), close all jupyter notebook tabs and reopen it then convert the notebook to pdf

pip install --upgrade --user nbconvert

Solution 10:[10]

  1. Update/Reinstall Anaconda if not updated.
  2. now install chromium through link https://chromium.woolyss.com/download/en/ and make it default browser.
  3. Then run jupyter notebook using this browser.
  4. Lastly use option "Download as" in jupyter notebook and then select the option using PDF via pyppeteer (.html)

HOLA!! It worked..

Solution 11:[11]

How to install latex and xelatex on Mac so that Jupyter "Download as PDF" will work

brew install pandoc
brew tap homebrew/cask
brew cask install basictex
eval "$(/usr/libexec/path_helper)"
# Update $PATH to include `/usr/local/texlive/2020basic/bin/x86_64-darwin`
sudo tlmgr update --self
sudo tlmgr install texliveonfly
sudo tlmgr install xelatex
sudo tlmgr install adjustbox
sudo tlmgr install tcolorbox
sudo tlmgr install collectbox
sudo tlmgr install ucs
sudo tlmgr install environ
sudo tlmgr install trimspaces
sudo tlmgr install titling
sudo tlmgr install enumitem
sudo tlmgr install rsfs

More info here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nico
Solution 2 einsweniger
Solution 3 Mona Jalal
Solution 4
Solution 5 Yaakov Bressler
Solution 6
Solution 7
Solution 8 Henry Cooksley
Solution 9
Solution 10 Supriya
Solution 11 dembelet