Create a python script that install python modules and run some commands

自作多情 提交于 2021-02-04 21:55:47

问题


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

  1. installing some packages with pip
  2. run some command of the type jupyter enable ... or jupyter-kernelspec install
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!