# How to Fix Common pytest Errors with uv


pytest failures that have nothing to do with your actual tests are some of the most frustrating errors in Python development. This guide covers the problems people hit most often when running pytest in [uv](https://pydevtools.com/handbook/reference/uv.md)-managed projects and how to fix them.

## ModuleNotFoundError

The most common pytest error in uv projects. There are several causes.

### Missing `uv sync`

If you see `ModuleNotFoundError` for your own package, the project likely has not been installed into the virtual environment:

```
ModuleNotFoundError: No module named 'my_package'
```

Fix it by syncing your project:

```bash
uv sync
```

This installs your project and its dependencies into the virtual environment. After syncing, `uv run pytest` should find your package.

> [!TIP]
> `uv run` calls `uv sync` automatically before executing, so this issue usually appears only when you activate the virtual environment manually and run `pytest` directly.

### Flat Layout Import Issues

Projects that use a flat layout (source code at the repository root, not inside a `src/` directory) can fail because pytest does not add the project root to `sys.path` by default:

```
tests/test_app.py:1: in <module>
    from app import create_app
ModuleNotFoundError: No module named 'app'
```

Add `pythonpath` to your pytest configuration in `pyproject.toml`:

```toml
[tool.pytest.ini_options]
pythonpath = ["."]
```

This tells pytest to add the project root to `sys.path` before collecting tests, making top-level modules importable.

### Missing `__init__.py`

If your tests can import some modules but not others, check for a missing `__init__.py`:

```
my_package/
├── __init__.py
├── core.py
└── utils/          # <-- missing __init__.py
    └── helpers.py
```

Python will not treat `utils` as a package without `__init__.py` (unless you are using namespace packages intentionally). Add the file:

```bash
touch my_package/utils/__init__.py
```

## Choosing Between `uv run pytest`, `uvx pytest`, and `uv run --with pytest pytest`

These three commands look similar but behave differently:

| Command | Uses project deps? | Installs your package? | When to use |
|---|---|---|---|
| `uv run pytest` | Yes | Yes | Standard project testing |
| `uvx pytest` | No | No | Quick one-off test run in any directory |
| `uv run --with pytest pytest` | Yes | Yes | Project without pytest in its dependencies |

`uv run pytest` is the right choice for most projects. It runs pytest from the project's virtual environment, with your package installed and all dependencies available.

`uvx pytest` runs pytest in an isolated, temporary environment with no access to your project's dependencies. Use it for running pytest on standalone scripts that have no imports from your project.

`uv run --with pytest pytest` is useful when pytest is not listed in your project's dependencies but you still want to test against the project's installed packages.

## conftest.py Not Found

If fixtures or hooks defined in `conftest.py` are not being picked up, the file is likely in the wrong directory. pytest looks for `conftest.py` in the test directory and every parent directory up to the root:

```
my_project/
├── conftest.py          # Available to all tests
├── tests/
│   ├── conftest.py      # Available to tests in tests/
│   ├── test_api.py
│   └── integration/
│       ├── conftest.py  # Available to tests in integration/
│       └── test_e2e.py
```

A `conftest.py` inside `src/` or a subdirectory that pytest does not traverse will be ignored.

## Fixture Not Found Across Files

```
fixture 'db_connection' not found
```

Fixtures defined in one test file are not visible to other test files. If multiple test files need the same fixture, move it to a `conftest.py` file in a shared parent directory:

```python
# tests/conftest.py
import pytest

@pytest.fixture
def db_connection():
    conn = create_test_database()
    yield conn
    conn.close()
```

Any test file under `tests/` will now have access to `db_connection` without importing it.

## See Also

- [Setting up testing with pytest and uv](https://pydevtools.com/handbook/tutorial/setting-up-testing-with-pytest-and-uv.md) for a complete project setup walkthrough
- [How to run tests using uv](https://pydevtools.com/handbook/how-to/how-to-run-tests-using-uv.md) for basic test execution
- [pytest reference](https://pydevtools.com/handbook/reference/pytest.md) for an overview of pytest features

## Learn more

- [uv: A Complete Guide](https://pydevtools.com/handbook/explanation/uv-complete-guide.md) covers what uv does, how fast it is, the core workflows, and recent releases.
