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 pytest not recognizing the project module #9291

Open
EdmundsEcho opened this issue Nov 20, 2024 · 8 comments
Open

uv run pytest not recognizing the project module #9291

EdmundsEcho opened this issue Nov 20, 2024 · 8 comments
Labels
question Asking for clarification or support

Comments

@EdmundsEcho
Copy link

EdmundsEcho commented Nov 20, 2024

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.

@EdmundsEcho EdmundsEcho changed the title pytest not recognizing the module uv run pytest not recognizing the project module Nov 20, 2024
@zanieb
Copy link
Member

zanieb commented Nov 20, 2024

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

@EdmundsEcho
Copy link
Author

EdmundsEcho commented Nov 20, 2024

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
Copy link
Contributor

bluss commented Nov 20, 2024

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 zanieb added the question Asking for clarification or support label Nov 20, 2024
@zanieb
Copy link
Member

zanieb commented Nov 20, 2024

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
Copy link
Author

@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
Copy link
Member

zanieb commented Nov 21, 2024

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
Copy link
Author

@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
Copy link

medecau commented Nov 21, 2024

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking for clarification or support
Projects
None yet
Development

No branches or pull requests

4 participants