I have the following directory structure:
src
\conftest.py
\dir_A
\run_A.py
\test_run_A.py
\dir_B
\run_B.py
where run_A.py has the following code:
from dir_B import run_B
...
When I run pytest
from src
, I get the error:
ImportError while importing test module '/home/user/src/dir_A/run_A.py'
...
ImportError: cannot import name 'run_B'
Is there a reason why this fails despite using conftest.py?
I'd like to add that running python3 -m dir_A.test_run_A
from src
works perfectly fine as a test.
You'll find this link helpful: https://docs.pytest.org/en/latest/pythonpath.html
Essentially, when running these tests from the src
directory, pytest
will walk all the way down to /home/user/src/dir_A/run_A.py
. Without any __init__.py
files, it will then refuse to walk anywhere else, as it doesn't realize it is part of a larger package.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.