[Python] Module not found when using UV, py-shiny and external dependency

Discussão em 'Python' iniciado por Stack, Setembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    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?

    Version etc


    ❯ 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"

    Continue reading...

Compartilhe esta Página