-1

My python package depends on a static data file which is automatically generated from a smaller seed file using a function that is part of the package.

It makes sense to me to do this generation at the time of running setup.py install, is there a standard way in setup() to describe “run this function before installing this package's additional files” (the options in the docs are all static)? If not, where should I place the call to that function?

Anaphory
  • 6,045
  • 4
  • 37
  • 68

1 Answers1

1

Best done in two steps using the cmdclass mechanism:

  • add a custom command to generate the data file
  • override build_py to call that before proceeding
from distutils.cmd import Command

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


class GenerateDataFileCommand(Command):
    description = 'generate data file'
    user_options = []

    def run(self):
        pass  # Do something here...


class InstallCommand(install):

    def run(self):
        self.run_command('generate_data_file')
        return super().run()


setup(
    cmdclass={
        'generate_data_file': GenerateDataFileCommand,
        'install': InstallCommand,
    },
    # ...
)

This way you can call python setup.py generate_data_file to generate the data file as a stand-alone step, but the usual setup procedure (python setup.py install) will also ensure it's called.

(However, I'd recommend including the built file in the distribution archive, so end users don't have to build it themselves – that is, override build_py (class setuptools.command.build_py.build_py) instead of install.)

AKX
  • 152,115
  • 15
  • 115
  • 172