Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I run a script file as part of the python setup.py install?

Question

I know how to use setup.py with setuptools to register a script. How would I run another script file (let say a make file) as part of the python setup.py install.

Background

I imagine that I would use something like:

os.system('make maketarget') #from somewhere in the package

But setuptools.setup receives a dict so I can't just add this line inside setup()/ and I need the script to run after the basic package is installed by setup.py install.

I know I can add a command to setup.py but I want this script to be called inside the install step.

I can also default to just placing a:

if sys.argv[-1] == 'install':
    os.system('do something in the shell')

and just place this block after the setup(), but somehow this doesn't look very pytonic (and also error prone, I need to find where this package installed exactly etc)

like image 938
alonisser Avatar asked Mar 15 '13 18:03

alonisser


1 Answers

You can subclass the existing install command from setuptools and extend it with the desired functionality. Afterwards you can register it as the install command in the setup function:

from setuptools.command.install import install

class MyInstall(install):

    def run(self):
        install.run(self)
        print("\n\n\n\nI did it!!!!\n\n\n\n")

# in the setup function:
cmdclass={'install': MyInstall}

Instead of the print statement just call your script.

Please be aware that there are many ways of packaging python software (eggs, etc.). Your script won't be called for these ways if you only call it inside the install step.

like image 66
languitar Avatar answered Oct 03 '22 17:10

languitar