Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a single-file namespace library with uv?

Tags:

python

uv

I have a project structured like this:

<project-root>
  ├── pyproject.toml
  └── src
       └── a_namespace
               └── a_module.py

With Poetry, I can install and package this project using the following settings:

[tool.poetry]
packages = [
    { include = "a_namespace/a_module.py", from = "src" },
]

However, I can’t get this to work with uv. According to the documentation, I need to do this:

[tool.uv.build-backend]
module-name = "a_namespace.a_module"

But this results in the following error:

× Failed to build ... @ file:///...`
  ╰─▶ Expected a Python module at: src/a_namespace/a_module/__init__.py

Adding .py doesn't solve the problem.

[tool.uv.build-backend]
module-name = "a_namespace.a_module.py"
× Failed to build ... @ file:///...`
  ╰─▶ Expected a Python module at: src/a_namespace/a_module/py/__init__.py

import-names and import-namespaces don't seem to work either.

[project]
import-namespaces = ["a_namespace"]
import-names = ["a_namespace.a_module"]
like image 999
Nattōsai Mitō Avatar asked Oct 29 '25 03:10

Nattōsai Mitō


1 Answers

I just successfully installed the project with the following settings:

[tool.uv.build-backend]
module-name = "a_library.a_module"
namespace = true

I’ve been aware of this namespace option the whole time, but I’d been avoiding it because the documentation scared me with this warning:

Using namespace = true disables safety checks. Using an explicit list of module names is strongly recommended outside of legacy projects.

Edit: While I succeeded in the installation, packaging failed. I gave up on the non-__init__.py structure, and ended up with the following one.

<project-root>
  ├── pyproject.toml
  └── src
       └── a_namespace
               └── a_module
                     └── __init__.py
like image 123
Nattōsai Mitō Avatar answered Oct 31 '25 16:10

Nattōsai Mitō



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!