Skip to content

routes/ auto-import crashes when app is installed as a package (with uv tool) #688

Description

@vietspaceanh

Describe the bug

When a blacksheep app with a routes/ folder is installed via uv tool install, app = Application() crashes at import time, the app cannot start.

The expected behavior is the app starts normally when I call the binary installed via uv tool from anywhere.

Steps to reproduce

1. Minimal project files creation

mkdir -p repro/myapp/routes
cat > repro/pyproject.toml << 'EOF'
[project]
name = "myapp"
version = "0.1.0"
dependencies = ["blacksheep"]

[project.scripts]
myapp = "myapp.main:run"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
EOF
cat > repro/myapp/__init__.py << 'EOF'
EOF
cat > repro/myapp/main.py << 'EOF'
from blacksheep import Application

app = Application()

def run():
    pass
EOF
touch repro/myapp/routes/__init__.py
cat > repro/myapp/routes/home.py << 'EOF'
from blacksheep import get

@get("/")
async def home():
    return "Hello"
EOF

2. Install it as a tool

cd repro && uv tool install .

3. Run the binary from somewhere, let say in home ~ folder

cd ~ && myapp

And what I got:

Traceback (most recent call last):
  File "/home/vietspaceanh/.local/bin/myapp", line 4, in <module>
    from myapp.main import run
  File "/home/vietspaceanh/.local/share/uv/tools/myapp/lib/python3.12/site-packages/myapp/main.py", line 3, in <module>
    app = Application()
          ^^^^^^^^^^^^^
  File "/home/vietspaceanh/.local/share/uv/tools/myapp/lib/python3.12/site-packages/blacksheep/server/application.py", line 215, in __init__
    _auto_import_routes(parent_file)
  File "/home/vietspaceanh/.local/share/uv/tools/myapp/lib/python3.12/site-packages/blacksheep/server/application.py", line 1016, in _auto_import_routes
    _auto_import(parent_file, "routes")
  File "/home/vietspaceanh/.local/share/uv/tools/myapp/lib/python3.12/site-packages/blacksheep/server/application.py", line 1008, in _auto_import
    import_child_modules(controllers_path)
  File "/home/vietspaceanh/.local/share/uv/tools/myapp/lib/python3.12/site-packages/blacksheep/utils/meta.py", line 31, in import_child_modules
    __import__(stripped_path + "." + module)
ModuleNotFoundError: No module named '.local'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions