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 = ["."]
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744704416a4588967.html
sys.path
to src? – Coco Q. Commented Mar 13 at 11:39