pip uninstall上的自定义代码

scu*_*bbo 8 python pip setuptools setup.py

我想在运行时运行一些自定义代码pip uninstall,清理在安装时创建的文件.我该怎么办呢?

我在setup.py中使用以下命令运行自定义安装代码:

from setuptools import setup
from setuptools.command.install import install

class CustomInstallCommand(install):
  def run(self):
    #Custom code here
    install.run(self)
...
setup(
  ...
  cmdclass = {
    'install':CustomInstallCommand
  }
)
Run Code Online (Sandbox Code Playgroud)

但尝试类似的setuptools.command.uninstallfrom setuptools.command.install import uninstall失败的东西,因为那些模块/名称不存在.

sor*_*rin 3

正如其他人所说:强烈建议不要这样做 for security reasons and even if you find a way to do it now, is likely to break in the near feature.

这同样适用于安装命令,而不仅仅是卸载命令。所以,请不要走这条路。

Python 打包(包括 pip)将走向完全声明式,无需运行托管包中的任何代码。