[Python] How to Run uv Pytest Tool with Proper Dependencies and Module Paths?

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    Directory Structure


    project-root/

    ├── src/

    │ ├── file1.py

    │ ├── file2.py


    └── tests/

    │ ├── test_file1.py

    │ ├── test_file2.py

    ├── requirements.txt

    ├── pyproject.toml


    So basically, under the project root, I have the source codes as src/*.py, test scripts as tests/*.py and the requirements.txt that gives the python packages such as tensorflow==2.18.0

    Goal


    At the project root directory, run

    uv tool run pytest


    and all the tests from tests/*.py will be run to log the output to the console.

    Difficulties


    I am facing two difficulties here, related to the import path, and dependencies.

    Import Path


    Basically, as follows.

    # test_file1.py

    from file1 import Class1 # Fails, but this would be my first preference to import
    from src.file1 import Class1 # Still fails



    How to import classes and functions defined in src/*.py to the files in the tests? I tried

    uv pip install --editable ./


    The command run successfully, but did not help, and the classes in the source files are still not visible in the test directory.

    Dependency Resolution


    I am installing the pip dependencies with

    time uv pip install --requirement requirements.txt


    which is working for the source code. But it seems the pytest tool cannot see those dependencies, so it is complaining like ModuleNotFoundError: No module named 'tensorflow'

    Note


    As I said, uv is my preferred tool for packaging, so best if the answer sticks to this framework instead of introducing poetry or something.

    Continue reading...

Compartilhe esta Página