Python pip 的 `--no-cache-dir` 有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45594707/
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 17:09:07  来源:igfitidea点击:

What is pip's `--no-cache-dir` good for?

pythonpip

提问by Martin Thoma

I've recently seen the --no-cache-dirbeing used in a Docker file. I've never seen that flag before and the help is not explaining it:

我最近看到--no-cache-dir在 Docker 文件中使用。我以前从未见过那个标志,帮助也没有解释它:

 --no-cache-dir              Disable the cache.
  1. Question: What is cached?
  2. Question: What is the cache used for?
  3. Question: Why would I want to disable it?
  1. 问题:缓存是什么?
  2. 问题:缓存有什么用?
  3. 问题:我为什么要禁用它?

采纳答案by Stack

  1. Cached is: store away in hiding or for future use
  2. Used for
    • store the installation files(.whl, etc) of the modules that you install through pip
    • store the source files (.tar.gz, etc) to avoid re-download when not expired
  3. Possible Reasonyou might want to disable cache:
    • you don't have space on your hard drive
    • previously run pip installwith unexpectedsettings
      • eg:
        • previously run export PYCURL_SSL_LIBRARY=nssand pip install pycurl
        • want new run export PYCURL_SSL_LIBRARY=openssland pip install pycurl --compile --no-cache-dir
  1. 缓存是:隐藏起来或以备将来使用
  2. 用于
    • 存储.whl您通过 pip 安装的模块的安装文件(等)
    • 存储源文件(.tar.gz等)以避免在未过期时重新下载
  3. 您可能想要禁用缓存的可能原因
    • 你的硬盘上没有空间
    • 以前pip install使用意外设置 运行
      • 例如:
        • 以前运行export PYCURL_SSL_LIBRARY=nsspip install pycurl
        • 想要新的运行export PYCURL_SSL_LIBRARY=opensslpip install pycurl --compile --no-cache-dir

Links to documentation

文档链接

https://pip.pypa.io/en/stable/reference/pip_install/#caching– @emredjan https://pip.pypa.io/en/stable/reference/pip_install/- @mikea

https://pip.pypa.io/en/stable/reference/pip_install/#caching– @emredjan https://pip.pypa.io/en/stable/reference/pip_install/- @mikea

回答by Philip Tzou

I think there is a good reason to use --no-cache-dirwhen you are building Docker images. The cache is usually useless in a Docker image, and you can definitely shrink the image size by disabling the cache.

我认为在--no-cache-dir构建 Docker 镜像时有一个很好的理由使用它。缓存在 Docker 镜像中通常是无用的,您绝对可以通过禁用缓存来缩小镜像大小。

回答by PacketFiend

Another reason to disable the pip cache - if you run pip as a user that does not yet exist, their home directory will be created, but owned by root.

禁用 pip 缓存的另一个原因 - 如果您以尚不存在的用户身份运行 pip,则将创建其主目录,但由 root 拥有。

This happens to us when building Amazon AMIs in a chroot - pip is being run as a user that exists on the builder machine, but not in the chroot jail where the AMI is being constructed. This is problematic as that specific user can now not ssh to what was just built as their .ssh directory is not readable by them.

我们在 chroot 中构建 Amazon AMI 时会发生这种情况 - pip 以构建器机器上存在的用户身份运行,但不在构建 AMI 的 chroot jail 中运行。这是有问题的,因为该特定用户现在无法 ssh 到刚刚构建的内容,因为他们无法读取他们的 .ssh 目录。

I can't think of any other reason pip would be run as a user that doesn't exist though, so it's very much an edge case.

我想不出任何其他原因 pip 会以不存在的用户身份运行,因此这在很大程度上是一种边缘情况。

回答by Zoe The Paranoid

Reduce your docker image size if you're having python dependencies in your DockerFile, as your private registries/artifactories or your deployment servcies may have size limitation.

如果您的 DockerFile 中有 python 依赖项,请减小您的 docker 镜像大小,因为您的私有注册表/人工工厂或您的部署服务可能有大小限制。

回答by Harvey

I get permission error for installation of some pip packages if I don't use --no-cache-diroption.

如果我不使用--no-cache-dir选项,我会收到安装某些 pip 包的权限错误。

Building wheels for collected packages: pyyaml, bottleneck, nvidia-ml-py3
  WARNING: Building wheel for pyyaml failed: [Errno 13] Permission denied: '/home/user/.cache/pip/wheels/b1'
  WARNING: Building wheel for bottleneck failed: [Errno 13] Permission denied: '/home/user/.cache/pip/wheels/92'
  WARNING: Building wheel for nvidia-ml-py3 failed: [Errno 13] Permission denied: '/home/user/.cache/pip/wheels/7f'

chown /.cachefolder didn't help for some reason but with --no-cache-dirit works ok.

chown /.cache文件夹由于某种原因没有帮助,但--no-cache-dir它可以正常工作。