admin管理员组文章数量:1391951
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.
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.
1 Answer
Reset to default 2Complete answer can be found here;
But you can simply create _
init
_.py
file under tests
directory!
Secondary fix is to add this to your pyproject.toml
:
[tool.pytest.ini_options]
pythonpath = ["."]
本文标签: pythonHow to Run uv Pytest Tool with Proper Dependencies and Module PathsStack Overflow
版权声明:本文标题:python - How to Run uv Pytest Tool with Proper Dependencies and Module Paths? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744704416a2620753.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
sys.path
to src? – Coco Q. Commented Mar 13 at 11:39