radiac / nanodjango

Full Django in a single file - views, models, API ,with async support. Automatically convert it to a full project.
578 stars 26 forks source link

AttributeError running via uv run #21

Closed brunns closed 2 months ago

brunns commented 2 months ago

Almost certainly an error on my side, but I can't see it. My script looks like this:

# /// script
# dependencies = ["nanodjango"]
# ///

from nanodjango import Django

app = Django()

@app.route("/")
def hello(request):
    return "Hello World!"

if __name__ == "__main__":
    app.run()

Running it with uv (version 0.4.16) I get:

$ uv run nd_hello.py
Reading inline script metadata from: nd_hello.py
Traceback (most recent call last):
  File "/Users/brunns/temp/nd_hello.py", line 14, in <module>
    app.run()
  File "/Users/brunns/.cache/uv/archive-v0/rtJTHAUnOiRDZKIBA0bwC/lib/python3.12/site-packages/nanodjango/app.py", line 372, in run
    self._prepare(is_prod=False)
  File "/Users/brunns/.cache/uv/archive-v0/rtJTHAUnOiRDZKIBA0bwC/lib/python3.12/site-packages/nanodjango/app.py", line 295, in _prepare
    or getattr(sys.modules["__main__"], self._instance_name) != self
                                        ^^^^^^^^^^^^^^^^^^^
AttributeError: 'Django' object has no attribute '_instance_name'
xdrdak commented 2 months ago

It appears you are attempting to run the app via uv run nd_hello.py.

Assuming you are in a uv initialized project, you can use uv run nanodjango run nd_hello.py.

Otherwise: uvx nanodjango run nd_hello.py would work as well.

radiac commented 2 months ago

Thanks for spotting this! You should be able to uv run nd_hello.py, but there is a problem - it must have crept into a recent release, and there's a glaring lack of tests around this!

I'll be taking a look at it in the DjangoCon sprints over the next couple of days.

brunns commented 2 months ago

Thanks @radiac!