使用 UV、py-shiny 和外部依赖时找不到模块

问题描述 投票:0回答:1

我想创建一个闪亮的应用程序(使用 py-shiny),在其中使用

uv
管理依赖项。

到目前为止我已经使用了以下代码

uv init shiny-tester
cd shiny-tester
uv add shiny
vim app.py # see below
uvx shiny run # works as expected

我将以下内容写入

app.py
(直接取自官方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)

使用

uvx shiny run
可以按预期工作,并且应用程序可以运行。

当我尝试添加任何其他包时,例如通过将

numpy
插入
import numpy
来添加
app.py
,我收到错误消息,无法找到该模块,因为我尚未安装它。 为了解决这个问题,我运行了
uv add numpy
,它可以工作并且不会抛出任何错误。

当我运行

uvx shiny run
时,我仍然收到错误

  File "/mnt/c/Users/david/Desktop/shiny-tester/app.py", line 2, in <module>
    import numpy
ModuleNotFoundError: No module named 'numpy'

当我运行

uv run python
import numpy
时,可以找到并加载模块。

知道什么可能导致此问题以及如何解决它吗?

版本等

❯ 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"
python py-shiny uv
1个回答
0
投票

uvx tool
tool
安装到临时的隔离环境中
。这意味着运行您的应用程序的
shiny
可执行文件与虚拟环境中的可执行文件不同。

uv run
就是你想要的:

$ 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)
© www.soinside.com 2019 - 2024. All rights reserved.