python - How to Run uv Pytest Tool with Proper Dependencies and Module Paths? - Stack Overflow

Directory Structureproject-root├── src│ ├── file1.py│ ├── file2.py└── tests│ ├── test_file1.py│ ├──

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.

Share Improve this question asked Mar 13 at 11:18 DellaDella 1,6483 gold badges26 silver badges50 bronze badges 1
  • Have you tried adding sys.path to src? – Coco Q. Commented Mar 13 at 11:39
Add a comment  | 

1 Answer 1

Reset to default 2

Complete 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 = ["."]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744704416a4588967.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信