astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
20.3k stars 603 forks source link

ModuleNotFoundError after changing src/ in project lib template #7472

Open 5183nischal opened 1 day ago

5183nischal commented 1 day ago

Hi, Thanks for uv!

I initialize a project with uv init --lib trials, then go to the src/trials in the project folder add a file add.py with: def add(a, b): return a+b and add from .add import add to __init__.py and then run uv run python followed by import trials and i get: Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'trials1' wheras if I had done the same without before adding the file add.py to src/trials, it would work. I'm somewhat new to all this so maybe I'm missing something, what is expected and how can I make import trials work? Thank you!

zanieb commented 1 day ago

I couldn't reproduce this as described. Where is "trials1" coming from?

❯ uv init --lib trials
Initialized project `trials` at `/Users/zb/workspace/uv/trials`
❯ cd trials
❯ uv run python -c "import trials"
Using Python 3.13.0rc2
Creating virtualenv at: .venv
   Built trials @ file:///Users/zb/workspace/uv/trials
Installed 1 package in 0.94ms
❯ echo 'def add(): pass' > src/trials/add.py
❯ uv run python -c "import trials"
❯ echo 'from .add import add' > src/trials/__init__.py
❯ uv run python -c "import trials"
5183nischal commented 1 day ago

"trials1" was a typo! Ok, but it gets even stranger. I initialized a project, cd'd into it and then run uv run python -c "import trials", it works once and then suddenly throws the error when I run it again immediately (see the screenshot),

CleanShot 2024-09-17 at 9  30 34@2x

and if I make a project anywhere under ~/Documents/ I get the same problem, but for example if i make a project in ~/Downloads/, I don't get this problem and things are fine. What could be the reason?

zanieb commented 1 day ago

Uhh I have no clue. Sounds like something is removing the files? Is it still there if you do ls src/trials after the failure?

5183nischal commented 1 day ago

Yes, it's all there, nothing is deleted:

CleanShot 2024-09-17 at 9  41 58@2x

In fact, I simply copied trials to ~/Downloads and then suddenly the problem is gone..

zanieb commented 1 day ago

It might help to share the verbose output (with -v) for the error case

5183nischal commented 1 day ago

The verbose output looks like:

DEBUG uv 0.4.10
DEBUG Found project root: `/Users/nisch/Documents/projects_uv/trials`
DEBUG No workspace root found, using project root
DEBUG Discovered project `trials` at: /Users/nisch/Documents/projects_uv/trials
DEBUG Reading requests from `/Users/nisch/Documents/projects_uv/trials/.python-version`
DEBUG The virtual environment's Python version satisfies `Python 3.10`
DEBUG Using request timeout of 30s
DEBUG Found static `pyproject.toml` for: trials @ file:///Users/nisch/Documents/projects_uv/trials
DEBUG No workspace root found, using project root
DEBUG Existing `uv.lock` satisfies workspace requirements
Resolved 1 package in 9ms
DEBUG Using request timeout of 30s
WARN Failed to read metadata for file: No such file or directory (os error 2)
WARN Failed to read metadata for file: No such file or directory (os error 2)
DEBUG Requirement already installed: trials==0.1.0 (from file:///Users/nisch/Documents/projects_uv/trials)
Audited 1 package in 1ms
DEBUG Using Python 3.10.15 interpreter at: /Users/nisch/Documents/projects_uv/trials/.venv/bin/python3
DEBUG Running `python -c import trials`
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'trials'
DEBUG Command exited with code: 1

in the case that it works, the output is identical up until the error.