Pytest suddenly cannot find module
Hello. I use pytest on a CI server to test our software. We have been using pytest for years but with the latest update we have an error importing a module during pytest. My logic behind isolating this issue to pytest is: on the CI server, nothing has changed (same python, same code, same OS image) the updated version of pytest is the only thing that has changed.
original versions of what pytest passed with platform linux2 -- Python 2.7.12, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
current versions where I get failures: platform linux2 -- Python 2.7.12, pytest-3.7.1, py-1.5.4, pluggy-0.7.1
Let's say the module that cannot import is called X. When within the directory of X, (where the _UnitTest directory is) running pytest allows all of the tests to pass. Changing directory back one layer (or anywhere outside of the X directory), now those tests from module X fail because the import statement failed.
Also on macOS I had all of the tests passing with pytest 3.6.3. Upgraded pytest, and now the tests related to module X fail. Many other modules import and work however module X does not. I have debugged a lot of it, and all of the paths look good, but it still fails to import. I'm not exactly sure what sets module X apart from the other dozens of module we use successfully.
I hope this makes sense and I can elaborate more if needed. I am not working in a virtualenv but I can put full pip lists in the comments if more is needed other then the versions I gave above.
Thank you
Adam
@adamhrosenberg can you post that traceback, hopefully that'll give me some clues on what's going on.
I am also encountering a similar sounding issue on 3.7.2. I'll try to figure out a reduced test case.
3.7.2 has fixed the issue for me.
Need a help...( I am very new to python) I am not getting pytest.assume I have imported import pytest and trying to use pytest.assume as mentioned in the doc, unable to use it Can someone help me out..
@GiriManas pytest-assume works as expected.
$ cat test-assume.py
import pytest
@pytest.mark.parametrize(('x', 'y'), [(1, 1), (1, 0), (0, 1)])
def test_simple_assume(x, y):
pytest.assume(x == y)
pytest.assume(True)
pytest.assume(False)
$ sudo docker run -ti --rm -v "$(pwd)/test-assume.py:/root/test-assume.py" python:3 bash -c "pip install pytest-assume > /
dev/null 2>&1 && cd /root && ls && pytest test-assume.py"
test-assume.py
================================================================ test session starts =================================================================
platform linux -- Python 3.7.4, pytest-5.1.1, py-1.8.0, pluggy-0.12.0
rootdir: /root
plugins: assume-2.2.0
collected 3 items
test-assume.py FFF [100%]
====================================================================== FAILURES ======================================================================
______________________________________________________________ test_simple_assume[1-1] _______________________________________________________________
tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = None
def reraise(tp, value, tb=None):
try:
if value is None:
value = tp()
if value.__traceback__ is not tb:
> raise value.with_traceback(tb)
E pytest_assume.plugin.FailedAssumption:
E 1 Failed Assumptions:
E
E test-assume.py:7: AssumptionFailure
E >> pytest.assume(False)
E AssertionError: assert False
/usr/local/lib/python3.7/site-packages/six.py:692: FailedAssumption
______________________________________________________________ test_simple_assume[1-0] _______________________________________________________________
tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = None
def reraise(tp, value, tb=None):
try:
if value is None:
value = tp()
if value.__traceback__ is not tb:
> raise value.with_traceback(tb)
E pytest_assume.plugin.FailedAssumption:
E 2 Failed Assumptions:
E
E test-assume.py:5: AssumptionFailure
E >> pytest.assume(x == y)
E AssertionError: assert False
E
E test-assume.py:7: AssumptionFailure
E >> pytest.assume(False)
E AssertionError: assert False
/usr/local/lib/python3.7/site-packages/six.py:692: FailedAssumption
______________________________________________________________ test_simple_assume[0-1] _______________________________________________________________
tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = None
def reraise(tp, value, tb=None):
try:
if value is None:
value = tp()
if value.__traceback__ is not tb:
> raise value.with_traceback(tb)
E pytest_assume.plugin.FailedAssumption:
E 2 Failed Assumptions:
E
E test-assume.py:5: AssumptionFailure
E >> pytest.assume(x == y)
E AssertionError: assert False
E
E test-assume.py:7: AssumptionFailure
E >> pytest.assume(False)
E AssertionError: assert False
/usr/local/lib/python3.7/site-packages/six.py:692: FailedAssumption
================================================================= 3 failed in 0.31s ==================================================================
I am facing this today on GitHub. Test run locally.
Run pip install pytest
Requirement already satisfied: pytest in /opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages (6.2.5)
Requirement already satisfied: iniconfig in /opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages (from pytest) (1.1.1)
Requirement already satisfied: toml in /opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages (from pytest) (0.10.2)
Requirement already satisfied: packaging in /opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages (from pytest) (21.0)
Requirement already satisfied: pluggy<2.0,>=0.12 in /opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages (from pytest) (1.0.0)
Requirement already satisfied: py>=1.8.2 in /opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages (from pytest) (1.10.0)
Requirement already satisfied: attrs>=19.2.0 in /opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages (from pytest) (21.2.0)
Requirement already satisfied: pyparsing>=2.0.2 in /opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/site-packages (from packaging->pytest) (2.4.7)
============================= test session starts ==============================
platform linux -- Python 3.8.11, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: /home/runner/work/ms-mint/ms-mint, configfile: setup.cfg
plugins: dash-2.0.0, cov-2.12.1
collected 0 items / 15 errors
==================================== ERRORS ====================================
________________ ERROR collecting tests/test__MINT_notebook.py _________________
ImportError while importing test module '/home/runner/work/ms-mint/ms-mint/tests/test__MINT_notebook.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/hostedtoolcache/Python/3.8.11/x64/lib/python3.8/importlib/__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests/test__MINT_notebook.py:1: in <module>
from ms_mint.notebook import Mint
E ModuleNotFoundError: No module named 'ms_mint'
Hi @sorenwacker,
Only that doesn't give us enough information to help you, but basically pytest cannot import ms_mint.
How are you installing ms_mint in GitHub Actions (I suppose that's what you mean by "GitHub")?
Hm, that is the package that the tests should test and I did not change the github actions file. It was never necessary to install that package. I will try to edit the github actions file and explicitly install the package.
Yes, that worked. I added this to the gh-actions file:
pip install .
Thanks for your time.