python-poetry / poetry

Python packaging and dependency management made easy
https://python-poetry.org
MIT License
31.61k stars 2.26k forks source link

Saving custom scripts in pyproject.toml (alias npm run) #2496

Open bersace opened 4 years ago

bersace commented 4 years ago

Feature Request

What do you think of having a scripts described in pyproject.toml ? Something like Procfile or script section in packages.json.

Here is a suggestion of configuration & usage:

[tools.poetry.proc]
build = "python manage.py collectstatic"
test = "pytest tests/unit/"
serve = "flask run"
$ poetry proc build
...
$ poetry proc serve
...

I think it would help project to not require Makefile nor tox to share commands and in the same time, ensure commands are properly executed in selected environment.

What do you think of this ?

SanketDG commented 4 years ago

See https://github.com/python-poetry/poetry/issues/2310 for a broader spec.

bersace commented 4 years ago

@SanketDG I think it's something else.

abn commented 4 years ago

This has been discussed in #591 which was closed with https://github.com/python-poetry/poetry/pull/591#issuecomment-504762152.

As was stated there, this might be a candidate feature for a plugin once we have that baked into poetry >=v1.1.0.

bersace commented 4 years ago

@abn yes, that's exactly the subject of this PR :-) A plugin is a good fit for this feature.

nat-n commented 4 years ago

@bersace in the mean time I'd suggest checking out a project I'm working on that solves this problem in a slightly different way

Poe the Poet a task runner that works well with poetry.

It provides a flexible system for defining tasks in the pyroject.toml and executing them with a dedicated executable. Basic usage looks like:

Example configuration with different task types:

[tool.poe.tasks]
test       = "pytest --cov=poethepoet"                                # simple command based task
mksandwich = { script = "my_package.sandwich:build" }                 # python script based task
tunnel     = { shell = "ssh -N -L 0.0.0.0:8080:$PROD:8080 $PROD &" }  # shell script based task

Example invocation:

# extra argument is passed to the underlying pytest command
poe test -v   

# If poethepoet is added as a dev-dependency
poetry run poe tunnel

And it supports zsh command completion!

EDIT: as of v0.12.0 it can also be used as a poetry plugin

stranger-danger-zamu commented 2 years ago

Leaving my 2 cents here on the whole "scripts in pyproject.toml" conversation. I understand that this will probably not going to be implemented or accepted, but at least I added my 2 cents.


This is a clearly desired feature and has had well supported PRs demonstrating community support for it going back a few years.

While I do get the maintainers wanting this sort of functionality outside of poetry's core codebase and scope, people are asking for it because they see poetry as Python's primary DX/DI. The birds eye view of Python's developer experience at the moment is that pyproject.toml will be the future of Python and poetry is in the position as the developer interface for pyproject.toml projects because it's the most full featured tool that uses it that also builds.

Taking a step back, I personally just want a tool that I allows me to have a known procedure for all (modern) python projects.

Rust has this with cargo where the requisite to get up and running to contribute to a project is cloning the repo, having rust and cargo, making some changes, and running cargo build.

Go has something similar (if not a little messy due to older codebases possibly not supporting modules). That said it's simple in those languages since at most, they statically compile their artifacts and are done in terms of bundling their dependencies with their artifact.

Meanwhile in Python-land, we might build a wheel with explicit python dependencies tied to hashes. But that might not bundle all the true dependencies (eg. system libraries).

So, we have to reach out for other artifact formats which do allow us to encapsulate all our dependencies (eg. Docker, deb, rpm, conda, OS golden images, etc). This is a bigger problem with Python packaging that is definitely outside of poetry's scope.

There are plenty of tools which can easily encode the procedures to bundle python wheels into a new artifact, but nine times out of ten they are excessive or they make poetry redundant and/or useless.

To be honest, what I don't want is having to write documentation on how to set up a dev environment including on installing N plugins before they clone down the repository. Writing python scripts to run "tasks" might work but it can quickly become a soup of arbitrary code (and probably bad security practices). And if I am going through all that effort, I might as well just use setuptools or flit if poetry doesn't really have differentiating utility built-in from them.

It just feels a bit odd. If poetry is a package manager, then it shouldn't also build packages. You don't use APT or DNF to build deb or rpm files.

Dilski commented 2 years ago

This feels like a good candidate for a poetry plugin?

neersighted commented 2 years ago

https://github.com/nat-n/poethepoet and https://github.com/illBeRoy/taskipy both exist to do this, and Poe has a Poetry plugin (taskipy will likely gain one)

Dilski commented 2 years ago

Oh perfect, thank you!

brunolnetto commented 1 year ago

I just wrote an issue on the subject, but you are ahead of me. Linguistics call these words aliases. You must be familiar with it yourselves on Git environment and setup file .git/config.

If it is not already shipped, I suggest the following syntax:

 $ poetry alias "hocus-pocus" "echo Abracadabra" 
 $ poetry hocus-pocus
 Abracadabra

Main drawback: poetry alias introduces on the very most some code management "fear" of "malicious command". It evokes the question: who would write such a malicious command against itself? Nevertheless, a conscious peer-review vanishes such intents away.