uv icon indicating copy to clipboard operation
uv copied to clipboard

`uv init` should create a runnable project

Open charliermarsh opened this issue 1 year ago • 8 comments

I got some feedback on Twitter that it'd be nice if uv init created something that you could then uv run and immediately see feedback, similar to how cargo new gives you a "Hello, world!" application. You shouldn't need to figure out how to change the project to make it runnable, etc.

charliermarsh avatar Aug 22 '24 23:08 charliermarsh

(Open to feedback, an idea.)

charliermarsh avatar Aug 22 '24 23:08 charliermarsh

Yes please! I wanted this but didn't want to require it for uv init to start.

zanieb avatar Aug 22 '24 23:08 zanieb

This is the perfect opportunity to unleash the puffin:

print(r"""
         .-"-.
        /  _uv\_     .---------------.
        \  \__))}> _/  Hello, World!  \
        ,) ." \    \__________________/
       /  (    \
     ,"   )    ;
     )   /     /
   ,/_."`  _.-`
    /_/`"\\___
         `~~~`
""")

hauntsaninja avatar Aug 23 '24 02:08 hauntsaninja

This is actually not bad...

charliermarsh avatar Aug 23 '24 02:08 charliermarsh

Wow!

zanieb avatar Aug 23 '24 12:08 zanieb

I think ideally this would be fully compatible with uv's "tools", so that e.g. this works directly without any extra hassle:

  • On one computer, I run uv init foo, and upload the newly created repository "foo" somewhere on Github (without any manual edits).
  • On another computer I run uv tool install git+https..., and I have got my newly created command-line tool "foo" installed there, and it directly works: I can run foo and it prints out "Hello world!" or something.
  • And now naturally I can add further functionality, push to Github, and run uv tool upgrade everywhere to get the latest features.

This would give a really low-threshold approach for everyone (including very beginners) to develop their custom command-line tools and install them conveniently on their own computers.

suomela avatar Aug 23 '24 12:08 suomela

I agree that being able to run the project directly after init would be a useful feature but in the meantime this is what I'm doing.

rye supported project.scripts which allowed you to create an alias or command to run your project: https://rye.astral.sh/guide/pyproject/#projectscripts

I might be mistaken but I didn't see anywhere in the uv documentation that this feature/config was supported but it seems that it is.

So, after you init a project you can add a section to the pyproject.toml to create a convenient run command:

~ $ mkdir foo
~ $ cd foo/
~/foo $ uv init
Initialized project `foo`
~/foo $ echo -e "\n[project.scripts]\nmain = 'foo:hello'" >> pyproject.toml
~/foo $ uv sync
~/foo $ uv run main
Hello from foo!

I'd love to hear if there's a better way to approach this but this works for me.

epequeno avatar Aug 26 '24 19:08 epequeno

Interesting, I didn't realize setting up [project.scripts] is enough; I thought I'd have to add print() or something in the hello() function for it to actually print something.

suomela avatar Aug 26 '24 19:08 suomela