3

Post-install script with Python setuptools

Exactly this question, but with Poetry and no Setuptools.

I want to run

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

when my package is installed. With Setuptools you could just modify setup.py, but in Poetry there specifically is no setup.py.

What I actually want to do is generate a default .mypackage_config file and place it somewhere useful. I don't see how to do this without arbitrary code, but Poetry does not allow arbitrary code for installation. Is there any way to do this?

Isaiah Shiner
  • 151
  • 3
  • 12
  • maybe this? https://python-poetry.org/docs/pyproject/#scripts – yedpodtrzitko Jan 18 '20 at 16:31
  • The 'scripts' section of Poetry is a kinda misleading. It's Poetry's version of setuptools 'entry_points', which let's you create command line entrances to your package. It won't run the scripts automatically and isn't meant for that. https://python-packaging.readthedocs.io/en/latest/command-line-scripts.html – Isaiah Shiner Jan 18 '20 at 17:36

1 Answers1

3

It's not currently possible (and probably won't ever be)

The entire idea of poetry is that a package can be installed without running any arbitrary Python code. Because of that, custom post-install scripts will probably never exist (from the author of poetry, the link you gave in your question).

What you could do instead

You could use setuptools, and modify the setup.py script, but it's probably easier to remove the need for a post-install script by removing the need for a default config file.

Most Python tools, such as black, tend to assume default config parameters, unless there are settings in a config file (such as a pyproject.toml file) that overrides them, e.g.:

[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 has a command jupyterhub --generate-config to generate a default config file (source).

If your package is a library, not a command-line tool, i.e. you use it by import-ing it in your Python code, I'd recommend just passing in the configuration as arguments to the constructor/functions, since that's the standard way of passing config to a library.

As an example, look at PySerial's API. You can configure the library by passing args to the constructor serial.Serial, e.g.:

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:
Alois Klink
  • 306
  • 2
  • 6
  • 1
    Somehow, it didn't occur to me to use default values unless otherwise overridden. A `--generate-config` command also makes a ton of sense. Some very good ideas. There really is no need to run arbitrary code at install time. – Isaiah Shiner Jan 28 '20 at 16:54
  • 1
    No worries! To be honest, the only reason it occurred to me is because I've seen how quite a few different Python packages (and some Linux programs) do it, and I just copied what I thought they did well in my answer. Good luck with developing your package! – Alois Klink Jan 30 '20 at 10:17