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

问题:pip的`–no-cache-dir`有什么用?

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

 --no-cache-dir              Disable the cache.
  1. 问题:缓存了什么?
  2. 问题:高速缓存用于什么?
  3. 问题:为什么要禁用它?

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

 --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?

回答 0

  1. 缓存为:藏匿起来以备将来使用
  2. 用于
  • 存储.whl通过pip安装的模块的安装文件(等)
  • 存储源文件(.tar.gz等),以免在未到期时重新下载
  1. 您可能要禁用缓存的可能原因
  • 您的硬盘驱动器上没有空间
  • 以前pip install意外的设置 运行
    • 例如:
      • 以前运行export PYCURL_SSL_LIBRARY=nsspip install pycurl
      • 希望新的运行export PYCURL_SSL_LIBRARY=opensslpip install pycurl --compile --no-cache-dir
  • 您想要使Docker映像尽可能小

链接到文档

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

  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
  1. Possible Reason you might want to disable cache:
  • you don’t have space on your hard drive
  • previously run pip install with unexpected settings
    • eg:
      • previously run export PYCURL_SSL_LIBRARY=nss and pip install pycurl
      • want new run export PYCURL_SSL_LIBRARY=openssl and pip install pycurl --compile --no-cache-dir
  • you want to keep a Docker image as small as possible

Links to documentation

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


回答 1

我认为在--no-cache-dir构建Docker映像时有充分的理由使用。缓存在Docker映像中通常是无用的,并且您可以通过禁用缓存来缩小映像大小。

I think there is a good reason to use --no-cache-dir when 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.


回答 2

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

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

我想不出任何其他原因来以不存在的用户身份运行pip,因此这是一个非常极端的情况。

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.

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.

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.


回答 3

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

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.


回答 4

如果我不使用--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 /.cache文件夹由于某种原因没有帮助,但--no-cache-dir可以正常工作。

I get permission error for installation of some pip packages if I don’t use --no-cache-dir option.

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 /.cache folder didn’t help for some reason but with --no-cache-dir it works ok.