You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue is a placeholder to gather feedback from users about the new --import-mode=importlib introduced in pytest 6.0.
It is our intention to change the default to importlib in future versions, and perhaps even eliminate the other --import-mode options (prepend and append).
It looks like the pytest-bdd plugin is incompatible with --import-mode=importlib:
_______________________________ ERROR collecting tests/end2end/features/test_backforward_bdd.py _______________________________
tests/end2end/features/test_backforward_bdd.py:21: in <module>
bdd.scenarios('backforward.feature')
.tox/py38-pyqt515/lib/python3.8/site-packages/pytest_bdd/scenario.py:296: in scenarios
features_base_dir = get_features_base_dir(module)
.tox/py38-pyqt515/lib/python3.8/site-packages/pytest_bdd/scenario.py:241: in get_features_base_dir
default_base_dir = os.path.dirname(caller_module.__file__)
E AttributeError: 'NoneType' object has no attribute '__file__'
ImportError while importing test module '/home/graingert/projects/relative-imports-import-mode-importlib/graingertspkg/tests/test_business_logic.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
graingertspkg/tests/test_business_logic.py:1: in <module>
from .. import business_logic
E ImportError: attempted relative import with no known parent package
Not sure they are broken, imports using importlib by design won't add modules to sys.modules or change sys.path, which explains why it doesn't work for your use case.
This however brings an important point, that importlib is not recommended for tests included with package code if one wants to use relative imports.
We should consider that as documentation, and also weight this use case in an eventual future decision of changing the default --importmode.
Activity
Introduce --import-mode=importlib
Introduce --import-mode=importlib
Introduce --import-mode=importlib
Introduce --import-mode=importlib
Introduce --import-mode=importlib
The-Compiler commentedon Jul 12, 2020
It looks like the
pytest-bdd
plugin is incompatible with--import-mode=importlib
:Relevant code:
Any idea why this could be happening?
nicoddemus commentedon Jul 13, 2020
I think
bdd.scenarios
assumes the module given as arguments are available insys.path
, which is not the case when--import-mode=importlib
.Perhaps this will work (untested):
schperplata commentedon Aug 31, 2020
Maybe this issue should be transfered here?
nicoddemus commentedon Aug 31, 2020
Thanks @schperplata, but it seems that's a different issue however (I replied there).
graingert commentedon Sep 1, 2020
refs: pytest-dev/py#203 (comment)
for me relative imports are broken when using
--import-mode=importlib
https://github.com/graingert/relative-imports-import-mode-importlib/blob/default/tests/test_foo.py#L1when doing inline tests, I often use relative imports:
https://github.com/graingert/relative-imports-import-mode-importlib/blob/default/graingertspkg/tests/test_business_logic.py#L1
nicoddemus commentedon Sep 1, 2020
Not sure they are broken, imports using
importlib
by design won't add modules tosys.modules
or changesys.path
, which explains why it doesn't work for your use case.This however brings an important point, that
importlib
is not recommended for tests included with package code if one wants to use relative imports.We should consider that as documentation, and also weight this use case in an eventual future decision of changing the default
--importmode
.76 remaining items