Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uv run --project doesn't allow resolving module imports on windows #12270

Open
leokeba opened this issue Mar 18, 2025 · 8 comments
Open

uv run --project doesn't allow resolving module imports on windows #12270

leokeba opened this issue Mar 18, 2025 · 8 comments
Labels
bug Something isn't working

Comments

@leokeba
Copy link

leokeba commented Mar 18, 2025

Summary

In my python project, I have a scripts folder where I keep various utilities that need to import code from the main app folder. When running it on macOS, I just use uv run scripts/test.py --project . and everything works as expected. On my windows system, when I try uv run scripts\test.py --project . i get the following error : ModuleNotFoundError: No module named 'app'.

I tried using an absolute path instead and various tricks to make it work, and also using the --folder argument instead, but nothing helps. Am I misunderstanding something or is this a bug ?

Platform

Windows 11 x86_64

Version

0.6.6

Python version

3.13.2

@leokeba leokeba added the bug Something isn't working label Mar 18, 2025
@leokeba leokeba changed the title uv run --project doesn't allow resolving module imports uv run --project doesn't allow resolving module imports on windows Mar 18, 2025
@nbaju1
Copy link

nbaju1 commented Mar 18, 2025

Isn't --project . the same as running without the flag, since the default behavior is to look for the project within the current directory? If so do you get the same result without it?

@leokeba
Copy link
Author

leokeba commented Mar 18, 2025

Isn't --project . the same as running without the flag, since the default behavior is to look for the project within the current directory? If so do you get the same result without it?

On macOS, running a script from a subfolder breaks module imports from the rest of the app as the python interpreter runs from the subfolder environment instead of the root project folder. The solution I found to this issue was using the --project . flag, but this only works on macOS.

On windows, both give the same result, which is the following error : ModuleNotFoundError: No module named 'app'

In my opinion, the best solution would be not needing to use the --project . flag at all regardless of the OS or subfolder structure, but I figure there might be a reason things work that way.

@nbaju1
Copy link

nbaju1 commented Mar 18, 2025

Not able to reproduce on Windows 11.

Folder structure:

project/
├── app/
|   └── foo.py
├── scripts/
|   └── test.py
└── pyproject.toml

foo.py

def bar():
    print("bar")

test.py

from app.foo import bar

bar()

Running uv run .\scripts\test.py (without or without --project .) from the root outputs bar.

@leokeba
Copy link
Author

leokeba commented Mar 18, 2025

I reproduced your example and still get the same error on Windows 11.

What are the contents of your pyproject.toml ?

@nbaju1
Copy link

nbaju1 commented Mar 18, 2025

pyproject.toml

[project]
name = "project"
version = "0.1.0"
description = "Project"
requires-python = ">=3.12"

Note that I have placed the venv inside the project root and it is active before running the command, if that matters.
Edit: Scratch that. Deleted the venv and recreated it, now I get the same error as you. Old venv on my end it seems.

@leokeba
Copy link
Author

leokeba commented Mar 18, 2025

The only difference is that I was using python 3.13 instead of 3.12, but i tried changing the version and I still get the same result.

I am using a .venv placed in the project root as well. Any ideas for a workaround ?

@nbaju1
Copy link

nbaju1 commented Mar 18, 2025

Adding this to the pyproject.toml file

[project.scripts]
test = "scripts.test:main"

[build-system]
requires = ["uv_build"]
build-backend = "uv_build"

[tool.uv.build-backend]
module-root = ""
module-name = "app"

(placing the bar() call in a main function in test.py) and running uv run test works though (in uv v0.6.7)

@leokeba
Copy link
Author

leokeba commented Mar 18, 2025

Thanks a lot, this solution does indeed fix all my issues on both OSes, and it is much more elegant than what I was doing before.

You can close this issue if everything else is expected behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants