astral-sh / uv

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

uv run pytest not recognizing the project module #9291

Open EdmundsEcho opened 1 week ago

EdmundsEcho commented 1 week ago

My project structure is:

project_name/module_name/parser.py
project_name/module_name/__init__.py
project_name/tests/test_parser.py

The test_parser.py references the module:

from module_name.parser import print_summaries

I can build and run the parser but I cannot seem to get uv run pytest to run without getting a module not found error.

Apologies for the hyper newby question here.

zanieb commented 1 week ago

You'll probably need to define a build system so your project is installed in the environment and discoverable by pytest.

EdmundsEcho commented 1 week ago

Thank you for the response. The link was helpful.

In the meantime, the following gets pytest to run from the project root:

set -x PYTHONPATH .; uv run pytest

Interestingly, ruff seems to resolve the dependency without the env setting.

bluss commented 1 week ago

It's possible that https://docs.pytest.org/en/stable/explanation/goodpractices.html could help here, they have a section for your case: Tests as part of application code

zanieb commented 1 week ago

I did some more reading here, and your case should work with:

❯ uv run -m pytest

or

❯ touch tests/__init__.py
❯ uv run pytest --pyargs my_module

as described in the pytest documentation.

EdmundsEcho commented 1 week ago

@zanieb Thank you!!

What you provided is definitely worth including in the documentation that describes setting up an application with uv.

Even better, it may be worthwhile to include a init command that includes the file structure for testing hello.py ("It works!"). This would require having an opinion on the file structure... which is good. I believe the structure that I set-up is a solid, straightforward norm, but I actually don't care :-)) What I mean by that is that whatever uv chooses to go with, I would go with... I want to build and setup quickly, including tests (just like rust, which includes a unit test in the initialized app).

zanieb commented 1 week ago

The problem is that, in Rust, there's a standardized test runner. In Python, there is not. We'd get complaints if we configured the project with pytest by default, though maybe we should anyway.

EdmundsEcho commented 1 week ago

@zanieb My take on this is that anyone with a POV has what they need (expertise) to make the change to how they like it. The goal of uv is to get a person up and running.

medecau commented 1 week ago

had same issue where pytest couldn't find the package after converting from poetry to uv

fixed by adding this to pyproject.toml:

[build-system]
requires = ['setuptools']
build-backend = 'setuptools.build_meta'