uv icon indicating copy to clipboard operation
uv copied to clipboard

✨ How to define custom commands like `tool.rye.scripts`

Open jd-solanki opened this issue 1 year ago • 7 comments
trafficstars

Hi 👋🏻

I'm really happy for UV. I always wanted better python ecosystem thanks all of you for your efforts.

I want to migrate from rye to UV as I'm starting new project and wanted to add custom command like uv run dev that executes uv run fastapi dev src/backend/main.py for me.

In Rye, I used to define it under scripts section:

[tool.rye.scripts]
dev = "uvicorn aitools.main:app --reload"
migration = "alembic revision --autogenerate -m"
migrate = "alembic upgrade head"
downgrade = "alembic downgrade -1"

However, It looks like UV don't have this feature or isn't documented yet.

jd-solanki avatar Aug 21 '24 06:08 jd-solanki

I think this is done in the [project.scripts] table.

See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#creating-executable-scripts

captnswing avatar Aug 21 '24 09:08 captnswing

It is not exactly the same. [project.scripts] requires a python file to be created to execute something.

To achieve this, I'm still using poethepoet inherited from poetry, and it just works.

But yes, this would be great to have inside uv.

jorgebodega avatar Aug 21 '24 09:08 jorgebodega

pdm does it like this:

[tool.pdm.scripts]
start = "flask run -p 54321"

patrick91 avatar Aug 21 '24 12:08 patrick91

Yeah we don't support this yet, but we plan to in some way.

zanieb avatar Aug 21 '24 12:08 zanieb

this sounds really useful.

my solution for this right now is leaving scripts in a bin directory which does the trick because i don't have to remember commands or reverse-i-search forever. would love to have something like this:

# ...

[tool.uv.scripts]
clean = "rm -rf build dist *_cache"
format = "ruff format"
check = "ruff check"
test = "pytest"
dev = "uvicorn app.main:app --reload"

[tool.uv.scripts.combos]
ok = ["clean", "format", "check", "test"]

# ...

anthonycorletti avatar Aug 22 '24 19:08 anthonycorletti

@anthonycorletti the combo stuff is interesting, mise does it like this:

[tasks.fetch-openapi]
run = "curl localhost:8000/api/v1/openapi.json -O"

[tasks.modify-openapi]
run = "node modify-openapi-operationids.js"
depends = ["fetch-openapi"]

but I wonder if this is scope creep :D

patrick91 avatar Aug 23 '24 10:08 patrick91

This would be nice to have so I don't need to have separate make/just files

ion-elgreco avatar Aug 25 '24 12:08 ion-elgreco

Duplicate of https://github.com/astral-sh/uv/issues/5903

my1e5 avatar Aug 27 '24 10:08 my1e5

Hi. I created a small, dependency-free tool to manage scripts directly from pyproject.toml: https://github.com/phihung/tomlscript

It works with all dependencies management tools. Tested with uv, pip and poetry.

Configuration example

[tool.tomlscript]
# Start dev server ==> this line is the documentation of the command
dev = "uv run uvicorn --port {port:5001} myapp:app --reload"

# Linter and test 
test = """
uv run ruff check
uv run pytest --inline-snapshot=review
"""

# Generate pyi stubs (python function) ==> Execute python function
gen_pyi = "mypackage.typing:generate_pyi"

setup_dev = """
uv sync
uv run mypy --install-types--non-interactive
aws s3 cp s3://my-bucket/model ./models
"""

Usage

alias tom="uvx tomlscript"

tom  # list commands
tom setup_dev # Setup dev env, install dependencies, etc
tom dev --port 8000
tom gen_pyi  --mode all  # execute python function with arguments

phihung avatar Oct 30 '24 14:10 phihung

any update?

Mai0313 avatar Jan 16 '25 03:01 Mai0313

@Mai0313 please don't ask for updates. See #9452.

This issue is being tracked in #5903

zanieb avatar Jan 16 '25 03:01 zanieb