文件未找到错误: [Errno 2] 没有这个文件或目录: 'latex': 'latex' (Python 3.6问题)

16
我正在尝试使用latex解释器来为图形标签进行标注。我使用matplotlib库生成我的图形。
我在寻找答案时遇到了问题。我看到很多答案建议将latex添加到路径中,我们该如何做呢?
我已经尝试安装Ghostscript,更新matplotlib等,但都没有成功。任何关于此问题的帮助都将不胜感激。
用于测试的代码片段:
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
## for Palatino and other serif fonts use:
#rc('font',**{'family':'serif','serif':['Palatino']})
rc('text', usetex=True)

import numpy as np
import matplotlib.pyplot as plt


# Example data
t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(4 * np.pi * t) + 2

plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.plot(t, s)

plt.xlabel(r'\textbf{time} (s)')
plt.ylabel(r'\textit{voltage} (mV)',fontsize=16)
plt.title(r"\TeX\ is Number "
          r"$\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!",
          fontsize=16, color='gray')
# Make room for the ridiculously large title.
plt.subplots_adjust(top=0.8)

plt.savefig('tex_demo')
plt.show()

以下是执行代码的结果:
Traceback (most recent call last):

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/IPython/core/formatters.py", line 341, in __call__
    return printer(obj)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py", line 241, in <lambda>
    png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/IPython/core/pylabtools.py", line 125, in print_figure
    fig.canvas.print_figure(bytes_io, **kw)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 2212, in print_figure
    **kwargs)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 513, in print_png
    FigureCanvasAgg.draw(self)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 433, in draw
    self.figure.draw(self.renderer)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/figure.py", line 1475, in draw
    renderer, self, artists, self.suppressComposite)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 2607, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/axis.py", line 1192, in draw
    renderer)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/axis.py", line 1130, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/text.py", line 922, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/text.py", line 309, in _get_layout
    ismath=ismath)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 232, in get_text_width_height_descent
    s, fontsize, renderer=self)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/texmanager.py", line 501, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/texmanager.py", line 365, in make_dvi
    texfile], tex)

  File "/Users/selih/anaconda3/lib/python3.6/site-packages/matplotlib/texmanager.py", line 335, in _run_checked_subprocess
    stderr=subprocess.STDOUT)

  File "/Users/selih/anaconda3/lib/python3.6/subprocess.py", line 356, in check_output
    **kwargs).stdout

  File "/Users/selih/anaconda3/lib/python3.6/subprocess.py", line 423, in run
    with Popen(*popenargs, **kwargs) as process:

  File "/Users/selih/anaconda3/lib/python3.6/subprocess.py", line 729, in __init__
    restore_signals, start_new_session)

  File "/Users/selih/anaconda3/lib/python3.6/subprocess.py", line 1364, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)

FileNotFoundError: [Errno 2] No such file or directory: 'latex': 'latex'

<Figure size 432x288 with 1 Axes>

我相信这是一个很常见的错误,但解决问题的答案并没有给我解决它的工具。有关如何将latex、dvipng添加到我的PATH中的任何帮助都将不胜感激。


重复未回答:https://dev59.com/Nqnka4cB1Zd3GeqPSbd9 - amanb
你可以将此发布为答案,以帮助其他遇到类似问题的人。 - amanb
SO鼓励自问自答。 - Alain Merigot
sys.path 是错误的变量,它对应于 os.environ['PYTHONPATH']。你想要操作的是 os.environ['PATH']。(是的,命名很令人困惑。) - tripleee
你的latex可执行文件在哪里?要知道这个,打开终端并运行 type -a latex - gniourf_gniourf
显示剩余2条评论
4个回答

11

我遇到了相同的问题,安装了这些包,问题就解决了。

sudo aptitude install texlive-fonts-recommended texlive-fonts-extra
sudo apt-get install dvipng

你也可以尝试这个答案:https://dev59.com/Nqnka4cB1Zd3GeqPSbd9#55137294


在Ubuntu 20.04上对我有效的是 sudo apt install texlive-latex-base(其中包括 latex 二进制文件)。 - erb

7

我有同样的问题,但是我已经安装了字体和LaTeX。问题在于LaTeX没有被包含在我的Python路径中。

您可以通过运行以下命令来检查Python是否可以访问LaTeX:

import subprocess
subprocess.check_call(["latex"])

如果仍然返回“FileNotFound”错误,则可以手动(临时)将latex添加到您的路径中,方法如下:
import os 
os.environ["PATH"] += os.pathsep + '/usr/bin'

注意:/usr/bin/latex 是Ubuntu和其他Linux系统中的latex位置。对于您可能会有所不同。如果您正在使用Linux,可以通过在命令行中运行以下命令来检查latex所在位置:
whereis latex

我已经在我的Mac上安装了LaTeX,但是运行whereis latex没有任何输出。 - Blade
对我有用!在Mac上输入“where latex”给了我/Library/TeX/texbin/ - Alejandro Daniel Noel

1
在Ubuntu 20.04中,我也需要执行所有这些步骤。
sudo apt-get install cm-super

1
如果在 Docker 容器中出现此错误,您需要在 Dockerfile 中添加以下命令,以便将这些软件包包含在新的 Docker 镜像中。
RUN apt-get -y --no-install-recommends install texlive-latex-base texlive-fonts-recommended texlive-fonts-extra texlive-latex-extra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接