创建一个安装python模块并运行一些命令的python脚本 [英] Create a python script that install python modules and run some commands

查看:71
本文介绍了创建一个安装python模块并运行一些命令的python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想围绕此库创建一个薄包装器 https://github.com/jupyter-incubator/sparkmagic#installation

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. 使用pip安装一些软件包
  2. 运行一些类型为 jupyter enable ... jupyter-kernelspec install
  3. 的命令我要生成并放置在正确位置的
  4. 自定义配置文件

我正在检查哪些选项必须使安装像调用脚本一样自动进行.到目前为止,我已经使用所有这些命令创建了一个bash脚本.这样,它就可以与简单的 install.sh 一起使用,但是我可能会遇到问题,因为我不知道要使用哪个python二进制文件.如果用户要为python2或python3安装lib,那么除非触摸脚本,否则他无法选择此选项.

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.

然后我尝试使用 setup.py 并覆盖 cmdclass 插入自定义代码

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,
},

...
)

,但自定义打印仅在运行 python setup.py install 时有效,而对于 pip install

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?

推荐答案

问题不是直接与 pip install 有关,而是与 wheels 有关.打包为wheel的项目不能具有自定义安装步骤.例如,请参见讨论.由于 pip 倾向于使用轮子,如有必要,请在本地构建轮子,因此自定义步骤不会在安装时运行.不过,可能有一些方法可以防止车轮踩踏.

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.

知道这一点之后,您可能应该遵循 Jan Vlcinsky 在他对以下问题的回答中的建议:

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.

指示软件包的用户在安装软件包后立即运行它( pip install myproject&& myprojectconfigure ).

Instruct the users of your package to run it immediately after installing your package (pip install myproject && myprojectconfigure).

最终考虑也要提供 myprojectconfigure --uninstall .

这篇关于创建一个安装python模块并运行一些命令的python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆