【问题标题】:FileNotFoundError: [Errno 2] No such file or directory: 'latex': 'latex' (Python 3.6 issue)FileNotFoundError: [Errno 2] No such file or directory: 'latex': 'latex' (Python 3.6 issue)
【发布时间】:2019-02-01 10:45:56
【问题描述】:

我正在尝试将乳胶解释器用于图形标签。我使用 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 添加到我的 PATH 的帮助将不胜感激。

【问题讨论】:

  • 您可以将其发布为其他面临类似问题的人的答案。
  • Answering own questions 受到 SO 的鼓励。
  • 不幸的是 import sys , sys.path.insert(0,'/usr/local/texlive/2018') 并没有像我想的那样解决问题。
  • sys.path 是错误的变量,它对应于os.environ['PYTHONPATH']。你想操纵os.environ['PATH']。 (是的,命名令人困惑。)

标签: python matplotlib latex rc


【解决方案1】:

我遇到了同样的问题,安装了这些包,问题就消失了。

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

您可能还想试试这个答案: https://stackoverflow.com/a/55137294/4448477

【讨论】:

  • 在 Ubuntu 20.04 上对我有用的是 sudo apt install texlive-latex-base(包括 latex 二进制文件)
【解决方案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

【讨论】:

  • 我的 Mac 上已经有乳胶,但运行 whereis latex 没有输出。
  • 为我工作! Mac 上的where latex 给了我/Library/TeX/texbin/
【解决方案3】:

在 ubuntu 20.04 中,我也必须这样做。

sudo apt-get install cm-super

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    相关资源
    最近更新 更多