Local package randomly becomes unimportable when using `uv run` in a project created with `uv init --package`

2 weeks ago 8
ARTICLE AD BOX

I'm seeing non-deterministic import failures when using uv to manage a Python project created with "uv init --package". After a fresh "uv sync", my local package is importable, but after running a few "uv run" commands it suddenly becomes unimportable with:

ModuleNotFoundError: No module named 'tfm_rag_municipal'

No files or config change between runs. Recreating ".venv", deleting "uv.lock", and running "uv sync" fixes it temporarily — but the problem always returns.


Project Setup


Project created using:

uv init --package tfm-rag-municipal

Project structure (standard src layout):

tfm-rag-municipal/ pyproject.toml src/ tfm_rag_municipal/ __init__.py example.py corpus_audit/ __init__.py schema.py corpusrecord_example.py tests/ test_example.py

Example module:

def add(a: int, b: int) -> int: return a + b

Test:

from tfm_rag_municipal.example import add def test_add_basic(): assert add(2, 3) == 5

pyproject.toml (relevant parts)


[project] name = "tfm-rag-municipal" version = "0.1.0" requires-python = ">=3.12" dependencies = [] [project.scripts] tfm-rag-municipal = "tfm_rag_municipal:main" [project.optional-dependencies] dev = ["pytest", "ruff", "pre-commit"] [build-system] requires = ["uv_build>=0.9.15,<0.10.0"] build-backend = "uv_build"

uv sync reports:

+ tfm-rag-municipal==0.1.0 (from file:///.../tfm-rag-municipal)

Minimal Reproducible Sequence


Fresh reset:

rm -rf .venv uv.lock src/tfm_rag_municipal.egg-info uv sync

Everything works:

uv run python -c "import tfm_rag_municipal" uv run pytest uv run python -m tfm_rag_municipal.corpus_audit.corpusrecord_example

Then, after repeating:

uv run python -c "import tfm_rag_municipal" uv run python -c "import tfm_rag_municipal" uv run python -c "import tfm_rag_municipal"

Eventually:

ModuleNotFoundError: No module named 'tfm_rag_municipal'

No code or config changes occur.


Environment


macOS (M1) Python 3.12.7 (via pyenv) uv (latest version) Reproducible with or without VS Code running

Things I've already checked


init.py exists Not using pip manually Not modifying PYTHONPATH Issue appears in plain terminal Removing .venv + uv.lock always fixes it temporarily

Question


Why is uv intermittently losing access to my local package, and how can I prevent this?

Specific questions:

How can I ensure a local package created via "uv init --package" stays importable across uv run invocations? Is my pyproject.toml missing something required by uv or uv_build? Is this a known issue with uv + uv_build + src layout on macOS?

Any explanation or workaround would be greatly appreciated.

Read Entire Article