11

I want to run some custom code when I run pip uninstall, cleaning up files that were created on installation. How should I go about this?

I've got custom install code running by using the following in 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
  }
)

But trying something similar for setuptools.command.uninstall or from setuptools.command.install import uninstall fails, since those modules/names don't exist.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
scubbo
  • 4,969
  • 7
  • 40
  • 71
  • 1
    Shouldn't all your files be removed by uninstallation? Please describe the exact problem you are trying to solve in more detail. – MattDMo Jun 06 '15 at 21:04
  • Sure - I'm downloading [phantomjs](http://phantomjs.org/) to a temporary directory to be able to use it, since, as far as I can tell, it's only available via NodeJS (http://stackoverflow.com/a/15699761/1040915), not via pip dependencies? – scubbo Jun 06 '15 at 21:19
  • @scubbo: Generally doing that sort of stuff is not encouraged in `setup.py`, because it will be fragile and platform-dependent and cause problems. This is mostly because there is no Python way to express native dependencies. Instead, just document native dependency installation instructions in your README. However, Distutils working group is working on this: https://mail.python.org/pipermail/distutils-sig/2015-April/026114.html – Mikko Ohtamaa Jun 07 '15 at 01:20
  • Otherwise you end up horror like this: https://github.com/psycopg/psycopg2/blob/master/setup.py – Mikko Ohtamaa Jun 07 '15 at 01:21
  • 1
    Got it. So, if I understand you correctly, there's no approved/recommended way to provide a "single-command install" for python modules that have non-pip dependencies (e.g. if I wanted to provide a module for a non-developer audience)? That's disappointing. Thanks for the link, I'll follow the discussion with interest. – scubbo Jun 07 '15 at 19:57

1 Answers1

3

As others stated: this is strongly discouraged for security reasons and even if you find a way to do it now, is likely to break in the near feature.

The same applies to setup install commands, not only uninstall ones. So, please don't go this route.

Python packaging (including pip) is going towards being totally delarative without running any code from the managed packages.

sorin
  • 161,544
  • 178
  • 535
  • 806