问题描述
有没有办法告诉 pip 永远不要为我的包创建轮子缓存?
Is there a way to tell pip never to create a wheel cache of my package?
我编写了一个内部使用的包,当使用 setup.py
中的 cmdclass
安装时,它会设置一些符号链接.如果我安装 sdist,这些安装后和开发后触发器运行良好,但如果我将包编译为轮子,它们就不会运行.为了解决这个问题,我确保只将 sdist 上传到我们内部的 PyPI.
I wrote a package for in-house use that sets up some symbolic links when installed using cmdclass
in the setup.py
. These post-install and post-develop triggers run fine if I install the sdist, but if I compile the package to a wheel, they do not run. To counter this, I make sure to only upload the sdist to our internal PyPI.
根据文档,pip安装时从 sdist 将其编译为轮子并缓存以备下次使用.这意味着该软件包第一次安装正确,但后续安装已损坏.
According to the documentation, when pip installs from an sdist it compiles it to a wheel and caches it for next time. This means that the package installs right the first time, but subsequent installs are broken.
我知道我可以使用 --no-cache-dir
运行 pip,但我想确保我团队中的每个人都可以正确获取包,而无需添加这样的标志.没有人会想要这个包的轮子.
I know I could run pip with --no-cache-dir
, but I want to be sure everyone on my team can get the package correctly without having to add flags like this. There is no situation where someone would want a wheel of this package.
是否可以在我的包中设置一个设置或配置变量来告诉 pip 永远不要将其缓存为轮子?
Is there a setting or config variable I can set in my package to tell pip never to cache it as a wheel?
推荐答案
据我所知,没有干净的方法可以做到这一点.你可以抓住机会玩肮脏.
There is no clean way of doing this that I know of. You can take your chances and play dirty.
受此启发 评论,您可以尝试使用如下所示的 setup.py
脚本(仅部分测试):
Inspired by this comment, you could try with a setup.py
script that looks like this (only partially tested):
#!/usr/bin/env python3
import setuptools
setuptools.setup(
name='LookMaNoWheels',
version='1.2.3',
cmdclass={'bdist_wheel': None},
)
这个想法是在尝试构建轮子时强制失败.发生这种情况时,pip 之类的工具往往会通过直接从可用的任何发行版安装(在您的情况下为 sdist)来恢复.
The idea is to force a failure when trying to build a wheel. When this happens, tools such as pip tend to recover from this by installing directly from whatever distribution is available instead (in your case a sdist).
更新
这样的事情将有助于提供信息更丰富的错误消息:
Something like this would help provide a more informative error message:
#!/usr/bin/env python3
import distutils.errors
import setuptools
bdist_wheel = None
try:
import wheel.bdist_wheel
class bdist_wheel(wheel.bdist_wheel.bdist_wheel):
def run(self, *args, **kwargs):
raise distutils.errors.DistutilsClassError("No!")
except ModuleNotFoundError:
pass
setuptools.setup(
name='LookMaNoWheels',
version='1.2.3',
cmdclass={'bdist_wheel': bdist_wheel},
)
这篇关于防止 pip 缓存包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!