代码之家  ›  专栏  ›  技术社区  ›  Isaiah Shiner

使用Python Poetry安装后脚本

  •  0
  • Isaiah Shiner  · 技术社区  · 5 年前

    Post-install script with Python setuptools

    正是这个问题,但有诗,没有设置工具。

    我想跑

    print('Installation finished, doing other things...')
    

    安装我的软件包时。使用Setuptools,您只需修改 setup.py 但在诗歌中,没有明确的 setup.py .

    我真正想做的是生成一个默认值 .mypackage_config 把它归档并放在有用的地方。如果没有任意代码,我不知道该怎么做,但诗歌 does not allow arbitrary code 用于安装。有什么办法吗?

    0 回复  |  直到 5 年前
        1
  •  10
  •   Alois Klink    5 年前

    这目前是不可能的(而且可能永远也不会)

    诗意的整个想法是,可以在不运行任何任意Python代码的情况下安装包。因此,自定义安装后脚本可能永远不会存在( from the author of poetry, the link you gave in your question ).

    你能做什么

    您可以使用setuptools,并修改 setup.py 脚本,但通过删除对默认配置文件的需求,可能更容易删除对安装后脚本的需求。

    大多数Python工具,例如 black ,倾向于采用默认配置参数,除非配置文件中有设置(例如 pyproject.toml 文件)覆盖它们,例如:

    [tool.black] # specifies this next section is the config for black
    line-length = 88 # changes some configuration parameters from the defaults
    target-version = ['py36', 'py37']
    

    Jupyter有一个命令 jupyterhub --generate-config 生成默认配置文件( source ).

    如果您的包是库,而不是命令行工具,即您通过以下方式使用它 import -在Python代码中,我建议只将配置作为参数传递给构造函数/函数,因为这是将配置传递给库的标准方式。

    举个例子,看看 PySerial's API 。您可以通过向构造函数传递参数来配置库 serial.Serial 例如:

    import serial
    with serial.Serial(
        '/dev/ttyUSB0', # first config param, the port
        19200, # second config param, the baudrate
        timeout=5, # sets the timeout config param
    ) as ser:
    
        2
  •  2
  •   rodfersou    3 年前

    在我目前的项目中,我们决定创建一个引导脚本,也许这是最好的方法

    推荐文章