I want to create a shiny app (using py-shiny) where I manage the dependencies using uv
.
So far I have used the following code
uv init shiny-tester
cd shiny-tester
uv add shiny
vim app.py # see below
uvx shiny run # works as expected
where I write the following to app.py
(directly taken from the official doc):
from shiny import App, reactive, render, ui
# will be used later
# import numpy
app_ui = ui.page_fluid(
ui.input_action_button("action_button", "Action"),
ui.output_text("counter"),
)
def server(input, output, session):
@render.text()
@reactive.event(input.action_button)
def counter():
return f"{input.action_button()}"
app = App(app_ui, server)
This works as expected using uvx shiny run
and the app runs.
When I try to add any other package, eg numpy
by inserting a import numpy
into app.py
, I get the error that the module could not be found because I didnt install it yet.
To fix this I run uv add numpy
, which works and does not throw any errors.
When I run uvx shiny run
however, I still get the error
File "/mnt/c/Users/david/Desktop/shiny-tester/app.py", line 2, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
When I run uv run python
and import numpy
the module can be found and loaded.
Any idea what might cause this and how to fix it?
❯ uv --version
uv 0.4.17
❯ cat pyproject.toml
[project]
name = "shiny-tester"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"numpy>=2.1.1",
"shiny>=1.1.0",
]
❯ head uv.lock
version = 1
requires-python = ">=3.12"
resolution-markers = [
"platform_system != 'Emscripten'",
"platform_system == 'Emscripten'",
]
[[package]]
name = "anyio"
version = "4.6.0"
uvx
is for global tools. uvx sometool
installs sometool
into a temporary, isolated environment. This means the shiny
executable running your app does not have access to your actual environment.
uv run
is what you want:
$ uv run shiny run
INFO: Started server process [11373]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)