RyeのworkspaceのpytestでModuleNotFoundError
code:ディレクトリ構造
.
├── .venv
├── awesome
│ ├── pyproject.toml
│ ├── src
│ └── tests
├── fabulous
│ ├── pyproject.toml
│ ├── src
│ └── tests
├── pyproject.toml
├── requirements-dev.lock
├── requirements.lock
└── src
└── rye_workspace_example
カレントディレクトリでrye run pytest
code:shell
% rye run pytest
============================= test session starts ==============================
platform darwin -- Python 3.12.0, pytest-8.0.1, pluggy-1.4.0
rootdir: /.../rye-workspace-example
collected 1 item / 1 error
==================================== ERRORS ====================================
_______________ ERROR collecting fabulous/tests/test_fabulous.py _______________
ImportError while importing test module '/.../rye-workspace-example/fabulous/tests/test_fabulous.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../.rye/py/cpython@3.12.0/install/lib/python3.12/importlib/__init__.py:90: in import_module
return _bootstrap._gcd_import(namelevel:, package, level) E ModuleNotFoundError: No module named 'tests.test_fabulous'
=========================== short test summary info ============================
ERROR fabulous/tests/test_fabulous.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.03s ===============================
単体でのrye run pytest awesome、rye run pytest fabulousは通る
--collect-onlyも渡すと
code:txt
collected 1 item / 1 error
<Dir rye-workspace-example>
<Dir awesome>
<Package tests>
<Module test_awesome.py>
<Function test_awesome>
awesomeとfabulousでtestsパッケージが同名
rye run pytest --import-mode importlib で解決
code:collectされるようになる
collected 2 items
<Dir rye-workspace-example>
<Dir awesome>
<Package tests>
<Module test_awesome.py>
<Function test_awesome>
<Dir fabulous>
<Package tests>
<Module test_fabulous.py>
<Function test_fabulous>
デフォルトはprependモード
awesome/testsのパスをsys.pathにprependしたということではないか
なのでfabulous/testsの中のモジュールが(awesome/testsを探しても)見つからなくて上記のエラー、という理解をした