Note: distutils
is deprecated and the accepted answer has been updated to use setuptools
I'm trying to add a post-install task to Python distutils as described in How to extend distutils with a simple post install script?. The task is supposed to execute a Python script in the installed lib directory. This script generates additional Python modules the installed package requires.
My first attempt is as follows:
from distutils.core import setup
from distutils.command.install import install
class post_install(install):
def run(self):
install.run(self)
from subprocess import call
call(['python', 'scriptname.py'],
cwd=self.install_lib + 'packagename')
setup(
...
cmdclass={'install': post_install},
)
This approach works, but as far as I can tell has two deficiencies:
- If the user has used a Python interpreter other than the one picked up from
PATH
, the post install script will be executed with a different interpreter which might cause a problem. - It's not safe against dry-run etc. which I might be able to remedy by wrapping it in a function and calling it with
distutils.cmd.Command.execute
.
How could I improve my solution? Is there a recommended way / best practice for doing this? I'd like to avoid pulling in another dependency if possible.
python setup.py install
, as well aspip install
, see: stackoverflow.com/questions/21915469/… – Abhorrent