Python 使用 pip install Matplotlib 时出现内存错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29466663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:37:30  来源:igfitidea点击:

Memory error while using pip install Matplotlib

pythonmatplotlib

提问by nishanth anand

I am using Python 2.7, If i try to install Matplotlib I am getting this error if i use "pip install matplotlib"

我正在使用 Python 2.7,如果我尝试安装 Matplotlib,如果我使用“pip install matplotlib”,我会收到此错误

 Exception:
  Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 232, in main
      status = self.run(options, args)
    File "/usr/local/lib/python2.7/dist-packages/pip/commands/install.py", line 339, in run
      requirement_set.prepare_files(finder)
    File "/usr/local/lib/python2.7/dist-packages/pip/req/req_set.py", line 355, in prepare_files
      do_download, session=self.session,
    File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 782, in unpack_url
      session,
    File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 667, in unpack_http_url
      from_path, content_type = _download_http_url(link, session, temp_dir)
    File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 843, in _download_http_url
      _download_url(resp, link, content_file)
    File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 615, in _download_url
      for chunk in progress_indicator(resp_read(4096), 4096):
    File "/usr/local/lib/python2.7/dist-packages/pip/utils/ui.py", line 46, in iter
      for x in it:
    File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 580, in resp_read
      decode_content=False):
    File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/response.py", line 256, in stream
      data = self.read(amt=amt, decode_content=decode_content)
    File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/response.py", line 186, in read
      data = self._fp.read(amt)
    File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/cachecontrol/filewrapper.py", line 54, in read
      self.__callback(self.__buf.getvalue())
    File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/cachecontrol/controller.py", line 205, in cache_response
      self.serializer.dumps(request, response, body=body),
    File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/cachecontrol/serialize.py", line 81, in dumps
      ).encode("utf8"),
  MemoryError"

What might the problem be? I am using raspberry Pi 2 with a 16gb SD card. I still have 8gb data free but still getting this error. Kindly help

可能是什么问题?我正在使用带有 16GB SD 卡的树莓派 2。我仍然有 8GB 的​​免费数据,但仍然收到此错误。请帮忙

回答by Andrey Sobolev

It seems that you have insufficient RAM to build matplotlib from scratch. To overcome that, either turn on swap:

您似乎没有足够的 RAM 从头开始​​构建 matplotlib。要克服这个问题,请打开交换:

# create swap file of 512 MB
dd if=/dev/zero of=/swapfile bs=1024 count=524288
# modify permissions
chown root:root /swapfile
chmod 0600 /swapfile
# setup swap area
mkswap /swapfile
# turn swap on
swapon /swapfile

Or, if you have raspbian installed on your SD card, you can install matplotlib from the repository:

或者,如果您在 SD 卡上安装了 raspbian,则可以从存储库安装 matplotlib:

apt-get install python-matplotlib

回答by David Wolever

This error is coming up because, it seems, pip's caching mechanism is trying to read the entire file into memory before caching it… which poses a problem in a limited-memory environment, as matplotlib is ~50mb.

出现此错误的原因似乎是 pip 的缓存机制试图在缓存之前将整个文件读入内存……这在内存有限的环境中造成了问题,因为 matplotlib 约为 50mb。

A simpler solution, until pip is patched to use a constant-space caching algorithm, is to run pipwith --no-cache-dirto avoid the cache:

一个简单的办法,直到PIP进行了修补,使用恒定的空间缓存算法,是运行pip--no-cache-dir避免缓存:

$ pip --no-cache-dir install matplotlib

回答by MrKsn

--no-cache-dirdidn't work for me. I just closed all apps and only then I was able to finish installation.

--no-cache-dir对我不起作用。我刚刚关闭了所有应用程序,然后才能完成安装。