'Python pytest hangs. For instance, "pytest --version" simply hangs

The following hangs:

PS C:\Users\Fowler> pytest --version  

Notes:

  • I am in Windows 10.
  • By hang, I mean at least 5 minutes of waiting for the pytest --version to return...
  • While waiting for pytest, python.exe is using 100% of a logical processor on my computer.
  • I uninstalled all python installations with windows installer and I reinistalled python 3.8.0 in an attempt to fix.
  • pytest only fails when I am not using a venv. So, pytest does work using a venv.
  • However, I can't use a venv with vscode, because debugging with venv gives a strange "Session-1 timed out waiting for debuggee to spawn" <-- you would think the word debuggee would be a nice clue, but not much found with that word on google. I am guessing this is a different problem, but maybe related?
  • In summary, I can't debug python with a venv, and I can't run pytest unit tests without a venv. Probably, these items are unrelated... But, because of this catch-22, I will be sooo grateful for any hints to fix either problem.

When I hit <ctrl-c> to break out of the pytest "hang", the following is displayed (but changes a little bit at the end each time?:

Traceback (most recent call last):
  File "c:\program files\python38\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\program files\python38\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\Scripts\pytest.exe\__main__.py", line 7, in <module>
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\_pytest\config\__init__.py", line 72, in main
    config = _prepareconfig(args, plugins)
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\_pytest\config\__init__.py", line 222, in _prepareconfig
    return pluginmanager.hook.pytest_cmdline_parse(
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\pluggy\hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\pluggy\manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\pluggy\manager.py", line 84, in <lambda>
    self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\pluggy\callers.py", line 203, in _multicall
    gen.send(outcome)
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\_pytest\helpconfig.py", line 89, in pytest_cmdline_parse
    config = outcome.get_result()
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\pluggy\callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\pluggy\callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\_pytest\config\__init__.py", line 742, in pytest_cmdline_parse
    self.parse(args)
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\_pytest\config\__init__.py", line 948, in parse
    self._preparse(args, addopts=addopts)
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\_pytest\config\__init__.py", line 896, in _preparse
    self.pluginmanager.load_setuptools_entrypoints("pytest11")
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\pluggy\manager.py", line 299, in load_setuptools_entrypoints
    plugin = ep.load()
  File "c:\program files\python38\lib\importlib\metadata.py", line 75, in load
    module = import_module(match.group('module'))
  File "c:\program files\python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\_pytest\assertion\rewrite.py", line 138, in exec_module
    _write_pyc(state, co, source_stat, pyc)
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\_pytest\assertion\rewrite.py", line 274, in _write_pyc
    with atomic_write(fspath(pyc), mode="wb", overwrite=True) as fp:
  File "c:\program files\python38\lib\contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\atomicwrites\__init__.py", line 156, in _open
    with get_fileobject(**self._open_kwargs) as f:
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\atomicwrites\__init__.py", line 173, in get_fileobject
    descriptor, name = tempfile.mkstemp(suffix=suffix, prefix=prefix,
  File "c:\program files\python38\lib\tempfile.py", line 332, in mkstemp
    return _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  File "c:\program files\python38\lib\tempfile.py", line 247, in _mkstemp_inner
    file = _os.path.join(dir, pre + name + suf)
KeyboardInterrupt

The next time try to run pytest --version and I hit <ctrl-c> it ends with:

Traceback (most recent call last):
  File "c:\program files\python38\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
...
...
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\atomicwrites\__init__.py", line 173, in get_fileobject
    descriptor, name = tempfile.mkstemp(suffix=suffix, prefix=prefix,
  File "c:\program files\python38\lib\tempfile.py", line 332, in mkstemp
    return _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  File "c:\program files\python38\lib\tempfile.py", line 248, in _mkstemp_inner
    _sys.audit("tempfile.mkstemp", file)
KeyboardInterrupt

The next time try to run pytest --version and I hit <ctrl-c> it ends with:

Traceback (most recent call last):
  File "c:\program files\python38\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
...
...
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\atomicwrites\__init__.py", line 173, in get_fileobject
    descriptor, name = tempfile.mkstemp(suffix=suffix, prefix=prefix,
  File "c:\program files\python38\lib\tempfile.py", line 332, in mkstemp
    return _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  File "c:\program files\python38\lib\tempfile.py", line 256, in _mkstemp_inner
    if (_os.name == 'nt' and _os.path.isdir(dir) and
  File "c:\program files\python38\lib\genericpath.py", line 42, in isdir
    st = os.stat(s)
KeyboardInterrupt

The next time try to run pytest --version and I hit <ctrl-c> it ends with:

Traceback (most recent call last):
  File "c:\program files\python38\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
...
...
  File "C:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\atomicwrites\__init__.py", line 173, in get_fileobject
    descriptor, name = tempfile.mkstemp(suffix=suffix, prefix=prefix,
  File "c:\program files\python38\lib\tempfile.py", line 332, in mkstemp
    return _mkstemp_inner(dir, prefix, suffix, flags, output_type)
  File "c:\program files\python38\lib\tempfile.py", line 250, in _mkstemp_inner
    fd = _os.open(file, flags, 0o600)
KeyboardInterrupt

I don't know if this output will help, but I thought it might be useful to see all the locations on my machine where python and/or pytest are installed:

PS C:\Users\Fowler> where.exe /r c:\ python
c:\Program Files\Amazon\AWSCLI\runtime\python.exe
c:\Program Files\Amazon\AWSSAMCLI\runtime\python.exe
c:\Program Files\MySQL\MySQL Workbench 8.0 CE\python.exe
c:\Program Files\Python38\python.exe
c:\Program Files\Python38\Lib\venv\scripts\nt\python.exe
c:\Users\Fowler\.vscode\extensions\lextudio.restructuredtext-116.0.0\out\python.js
c:\Users\Fowler\.vscode\extensions\teabyii.ayu-0.18.0\test\Python.py
c:\Users\Fowler\.vscode\extensions\yzane.markdown-pdf-1.4.1\node_modules\highlight.js\lib\languages\python.js
c:\Users\Fowler\AppData\Local\GitHubDesktop\app-2.2.2\resources\app\highlighter\mode\python.js
c:\Users\Fowler\AppData\Local\GitHubDesktop\app-2.2.3\resources\app\highlighter\mode\python.js
c:\Users\Fowler\AppData\Local\Google\Chrome\User Data\Default\Extensions\ngkhgikojglcgnckopipfdajaifmmnnc\4.1.34_0\python.js
c:\Users\Fowler\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\etc\apparmor.d\abstractions\python
c:\Users\Fowler\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\usr\share\bash-completion\completions\python
c:\Users\Fowler\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\usr\share\bash-completion\helpers\python
c:\Users\Fowler\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\usr\share\sosreport\sos\plugins\python.py
c:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\aniso8601\builders\python.py
c:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\_pytest\python.py
c:\Users\Fowler\Documents\vscodeProjects\playarea\.venv\Scripts\python.exe
c:\Windows\Installer\$PatchCache$\Managed\8B9C64EBE8DD53846B6846E46A14F5EE\3.7.2150\python.exe
c:\Windows\Installer\$PatchCache$\Managed\9CB0624238F6F8F469EAD6566412DD7F\3.7.2150\python.exe

PS C:\Users\Fowler> where.exe /r c:\ pytest
c:\Users\Fowler\AppData\Roaming\Python\Python38\Scripts\pytest.exe
c:\Users\Fowler\AppData\Roaming\Python\Python38\site-packages\pytest.py

And finally! Whew, in case this sheds any light, here is a picture showing the python process having fun eating up my CPU during pytest.py...

Picture showing the python process running during pytest hang

I would be oh, so grateful for any assistance or thoughts!



Solution 1:[1]

Fixed.

The answer appears to be

  1. Uninstall python via the windows apps and features
  2. Remove the c:\program files\python38 directory
  3. Remove the ..\AppData\Roaming\Python directory
  4. Reinstall

Not sure what the "root" problem was, but a total wipe of python fixed it. Note that the python windows installer does not remove enough python stuff.

Thank you @nneonneo for getting me thinking in the right direction.

Solution 2:[2]

I have the similar problem

after i install pytest-cov or allure-pytest, pytest will hang

if i uninstall them , everything is back to normal

I have tried to install different pytest version, It didn't help

but, I tried the method you provide(uninstall python, remove the diretories), it works~, great?

but, when I install allure-pytest again, it fails again.

finally, I found that, I can't install package in privilege cmd window

If I install them in privilege cmd windows, it will make pytest hang!

so, if anyone encounter the problem I have, you can try my way.

Solution 3:[3]

In my case, the package anyio, a dependency of JupyterLab, was causing this problem. It was installed at the administrator level. I uninstalled it and reinstalled at the user level and pytest started working again.

Solution 4:[4]

Using Ubuntu 18.04/20.04 + Python 3.8.2

My tests were hanging in a similar matter after showing results.

Removing pytest-cov didn't help.

What helped was moving from Python 3.8.2 to a newer version (like 3.8.12)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul Fowler
Solution 2
Solution 3 Asclepius
Solution 4 d.lime