Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post-install script with Python Poetry

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?

like image 370
Isaiah Shiner Avatar asked Jan 18 '20 16:01

Isaiah Shiner


People also ask

How do I run a Python script from a poem?

To run your script simply use poetry run python your_script.py . Likewise if you have command line tools such as pytest or black you can run them using poetry run pytest .

Does poetry install Python?

Poetry helps you declare, manage and install dependencies of Python projects, ensuring you have the right stack everywhere. It supports Python 2.7 and 3.5+. Note: Python 2.7 and 3.5 will no longer be supported in the next feature release (1.2). You should consider updating your Python version to a supported one.

How do I install a Python script?

Installing Python Packages with Setup.py To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.

What is post install script?

A post install script is an Apex class that implements the InstallHandler interface. This interface has a single method called onInstall that specifies the actions to be performed on installation. global interface InstallHandler { void onInstall(InstallContext context) }


1 Answers

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:
like image 59
Alois Klink Avatar answered Oct 12 '22 13:10

Alois Klink