问题
I want to create a thin wrapper around this library https://github.com/jupyter-incubator/sparkmagic#installation
As you can see in the link, the installation step requires
- installing some packages with pip
- run some command of the type
jupyter enable ...
orjupyter-kernelspec install
- custom configuration file that I want to generate and place in the correct place
I am checking which options I have to make the installation as automatic as calling a script. So far I have came up creating a bash script with all these commands. In this way it works with a simple install.sh
but I might have problems since I will not know which python binaries to use. If a user wants to install the lib for python2 or python3 he cannot have this choice unless he touches the script.
I have then tried to use setup.py
and override the cmdclass
to insert custom code
from setuptools import setup
from setuptools.command.develop import develop
from setuptools.command.install import install
class PostDevelopCommand(develop):
"""Post-installation for development mode."""
def run(self):
develop.run(self)
print("do things")
class PostInstallCommand(install):
"""Post-installation for installation mode."""
def run(self):
install.run(self)
print("do things")
setup(
...
install_requires=['sparkmagic'],
cmdclass={
'develop': PostDevelopCommand,
'install': PostInstallCommand,
},
...
)
but the custom prints worked only when running python setup.py install
but not for pip install
I would like to make this last option work but if this is not the right patter to do this kind of things, could you suggest me which way to go?
回答1:
The issue is not with pip install
directly but with wheels. Projects packaged as wheel can not have custom install steps. See for example this discussion. Since pip prefers working with wheels, building them locally if necessary, your custom steps are not run at install time. There are probably ways to prevent the wheel step though.
Knowing this, you probably should follow the advice from Jan Vlcinsky in his comment for his answer to this question: Post install script after installing a wheel.
Add a setuptools console entry point to your project (let's call it
myprojectconfigure
). Maybe you prefer using the distutils script feature instead.Instruct the users of your package to run it immediately after installing your package (
pip install myproject && myprojectconfigure
).Eventually think about providing
myprojectconfigure --uninstall
as well.
来源:https://stackoverflow.com/questions/58312692/create-a-python-script-that-install-python-modules-and-run-some-commands