FileNotFoundError:[Errno 2]没有这样的文件或目录:“latex”:“latex”(Python 3.6问题)

2024-04-20 00:36:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用乳胶解释器来标记图形。我使用matplotlib库生成图形。

我很难找到这个常见问题的答案。我看到许多答案建议应该在路径中添加乳胶,我们如何做到这一点?

我试过安装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>

我相信这是一个很常见的错误,但解决它的答案并没有给我解决它的工具。如果你能帮我加入乳胶,dvipng,我将不胜感激。


Tags: inpyselfmatplotliblibpackageslinesite
2条回答

我也有同样的问题,只是我已经安装了字体和乳胶。问题是我的python路径中没有包含乳胶。

您可以通过运行以下命令检查python是否可以访问latex:

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

如果仍然返回FileNotFound错误,则可以使用以下方法手动(临时)向路径添加乳胶:

import os 
os.environ["PATH"] += os.pathsep + '/usr/bin'

注意:/usr/bin/latex是用于ubuntu和其他linux系统的latex。可能对你不一样。如果您使用的是linux,可以通过运行(在命令行中)检查latex的位置:

whereis latex

我也遇到了同样的问题,安装了这些软件包,问题就解决了。

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

您还可以尝试以下答案: https://stackoverflow.com/a/55137294/4448477

相关问题 更多 >