astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
21.32k stars 624 forks source link

uv run not able to run with asyncio #7715

Closed ClementWalter closed 2 days ago

ClementWalter commented 2 days ago
uv --version
uv 0.4.9 (77d278f68 2024-09-10)

Just initializing a new project with uv init with this slightly modified pyproject.toml

[project]
name = "uv-async"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.10"
dependencies = []

[project.scripts]
hello = "hello:main"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["hello.py"]

With the default script it works

uv run hello
   Built uv-async @ file:///Users/clementwalter/Documents/uv-async
Uninstalled 1 package in 0.57ms
Installed 1 package in 1ms
Hello from uv-async!

but adding asyncio

import asyncio

async def main():
    print("Hello from uv-async!")

if __name__ == "__main__":
    asyncio.run(main())

it raises

<coroutine object main at 0x1029d1a80>
sys:1: RuntimeWarning: coroutine 'main' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Running the script regularly with python works as expected

uv run python hello.py                         
Hello from uv-async!
zanieb commented 2 days ago

Your entrypoint is defined as

[project.scripts]
hello = "hello:main"

So we're calling hello:main not running hello.py and hitting your asycnio.run block

ClementWalter commented 2 days ago

thanks so actually doing

import asyncio

async def _main():
    print("Hello from uv-async!")

def main():
    asyncio.run(_main())

if __name__ == "__main__":
    main()

works for both usages

zanieb commented 2 days ago

Yep! Thanks!