4

I already saw one old post regarding this topic - An error while trying to execute tests on python 3.10 with pytest,

I am having the same problem, Python 3.10.4 and pytest 7.1.2, when I start command:

$ pipenv run pytest

I get an error:

$ pipenv run pytest
============================= test session starts =============================
platform win32 -- Python 3.10.4, pytest-4.0.0, py-1.7.0, pluggy-0.8.0
rootdir: **DIR**, inifile:
collected 0 items / 1 errors

=================================== ERRORS ====================================
____________________ ERROR collecting test/test_person.py _____________________
<frozen importlib._bootstrap>:939: in _find_spec
    ???
E   AttributeError: 'AssertionRewritingHook' object has no attribute 'find_spec'

During handling of the above exception, another exception occurred:
**LOCAL_PATH**\.virtualenvs\iamdb-2ZawZA6J\lib\site-packages\py\_path\local.py:668: in pyimport
    __import__(modname)
<frozen importlib._bootstrap>:1027: in _find_and_load
    ???
<frozen importlib._bootstrap>:1002: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:941: in _find_spec
    ???
<frozen importlib._bootstrap>:915: in _find_spec_legacy
    ???
**LOCAL_PATH**\.virtualenvs\iamdb-2ZawZA6J\lib\site-packages\_pytest\assertion\rewrite.py:162: in find_module
    source_stat, co = _rewrite_test(self.config, fn_pypath)
**LOCAL_PATH**\.virtualenvs\iamdb-2ZawZA6J\lib\site-packages\_pytest\assertion\rewrite.py:412: in _rewrite_test
    co = compile(tree, fn.strpath, "exec", dont_inherit=True)
E   TypeError: required field "lineno" missing from alias
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.16 seconds ===========================

Anyone has a solution?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
John
  • 230
  • 2
  • 12

3 Answers3

4

I faced the same issue when using poetry. In my case, pytest was set to v5.2:

[tool.poetry.dev-dependencies]
pytest = "^5.2"

After updating pytest to ^7.1.2, things started working.

poetry add -D pytest@latest
[tool.poetry.dev-dependencies]
pytest = "^7.1.2"
Deep Panchal
  • 129
  • 1
  • 4
  • This helped me find the root cause on my project, which was the min python version of `3.4`, which apparently prevented poetry from using the latest pytest version. Upped it to `3.7` (because I can just do that) and it worked. – Markus Ressel Jul 09 '22 at 10:45
1

As per comment from Marco Bonelli, pytest had no correct version. So command:

pipenv update pytest

fixed the issue.

John
  • 230
  • 2
  • 12
0

In my case I had the recent pytest version 7.1.2.

  • Downgrading did not help.
  • Removing my pyproject.toml file also did not help. (Thought it might be a pylint configuration issue.)

In the error message another package occurred (that I did not use at all in that specific project but that was nevertheless installed in my environment): dash.

=> Updating dash to the latest version resolved the issue.

=> I have no idea why.

=> If pytest does not run, check if updating some other packages that occur in the error message helps.


PS C:\python_env\workspace\hello_world_with_tests> pytest
Traceback (most recent call last): 
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\runpy.py", line 196, in _run_module_as_main 
    return _run_code(code, main_globals, None,
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\Scripts\pytest.exe\__main__.py", line 7, in <module>
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\_pytest\config\__init__.py", line 187, in console_main
    code = main()
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\_pytest\config\__init__.py", line 145, in main
    config = _prepareconfig(args, plugins)
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\_pytest\config\__init__.py", line 324, in _prepareconfig
    config = pluginmanager.hook.pytest_cmdline_parse(
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\pluggy\_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\pluggy\_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\pluggy\_callers.py", line 55, in _multicall
    gen.send(outcome)
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\_pytest\helpconfig.py", line 102, in pytest_cmdline_parse
    config: Config = outcome.get_result()
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\pluggy\_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\pluggy\_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\_pytest\config\__init__.py", line 1016, in pytest_cmdline_parse
    self.parse(args)
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\_pytest\config\__init__.py", line 1304, in parse
    self._preparse(args, addopts=addopts)
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\_pytest\config\__init__.py", line 1187, in _preparse
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\pluggy\_manager.py", line 287, in load_setuptools_entrypoints
    plugin = ep.load()
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\importlib\metadata\__init__.py", line 171, in load
    module = import_module(match.group('module'))
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\_pytest\assertion\rewrite.py", line 168, in exec_module
    exec(co, module.__dict__)
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\dash\__init__.py", line 5, in <module>
    from .dash import Dash, no_update  # noqa: F401,E402
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\_pytest\assertion\rewrite.py", line 168, in exec_module
    exec(co, module.__dict__)
  File "C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\dash\dash.py", line 18, in <module>
ImportError: cannot import name 'get_current_traceback' from 'werkzeug.debug.tbtools' (C:\python_env\App\WinPython\python-3.10.4.amd64\lib\site-packages\werkzeug\debug\tbtools.py)
Stefan
  • 10,010
  • 7
  • 61
  • 117