Python pytest卡住了。例如,“pytest --version”只是卡住了。

6
以下是无响应的情况:
PS C:\Users\Fowler> pytest --version  

注意事项:

  • 我正在使用Windows 10。
  • 通过“挂起”,我指的是等待pytest --version返回至少5分钟...
  • 在等待pytest期间,python.exe在我的计算机上使用了100%的逻辑处理器。
  • 我使用Windows安装程序卸载了所有Python安装程序,并尝试重新安装了Python 3.8.0以修复问题。
  • 仅当我使用虚拟环境时,pytest才会失败。 因此,使用venv pytest确实有效。
  • 但是,在vscode中使用venv进行调试会出现奇怪的“Session-1超时等待debuggee生成”错误。虽然你可能认为“debuggee”这个词会是一个很好的提示,但在google上并没有发现太多与此相关的内容。我猜这是一个不同的问题,但也许有关连?
  • 总之,我无法使用venv调试Python,也无法在没有venv的情况下运行pytest单元测试。 可能,这些问题是不相关的...但是,由于这种进退两难的局面,我将非常感激任何解决这些问题的提示。

当我按下<ctrl-c>退出pytest的“挂起”状态时,会显示以下内容(每次结尾都会略有不同?):

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

下次尝试运行pytest --version,当我按下<ctrl-c>时,结果为:
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

下一次尝试运行 pytest --version 时,如果我按下<ctrl-c>,它会以以下方式结束:
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

下次尝试运行pytest --version,当我按下<ctrl-c>时,会以以下方式结束:

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

我不知道这个输出是否有用,但我认为查看我机器上安装了Python和/或Pytest的所有位置可能很有用:

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

最后!哇,为了帮助理解,这是一张照片,显示Python进程在pytest.py期间大量占用我的CPU...

显示pytest挂起时Python进程运行的图片

如果有任何帮助或想法,我将不胜感激!


1
也许你的全局 Python 中存在不同包之间的冲突或奇怪的交互,这就是为什么在 venv 中运行良好的原因。也许可以尝试逐步在 venv 中安装更多的全局包并进行测试,直到找出导致程序停顿的包? - nneonneo
好的、显而易见的想法,但是……我刚刚执行了 pip list 命令来创建 requirements.txt 文件。然后我创建了一个 venv 环境,激活它,并从 requirements.txt 中安装了完整的包列表(带有版本号),但它仍然在 venv 环境中工作???但在虚拟环境之外仍然失败。 - Paul Fowler
1
嗯,有点奇怪。这可能是一个仅在Windows上出现的问题,因为atomicwrites现在只在Windows上使用,而我没有Windows机器来测试任何东西。根据回溯信息,看起来pytest可能正在尝试写入大量文件。例如,您可以尝试编辑“C:\ Users \ Fowler \ AppData \ Roaming \ Python \ Python38 \ site-packages \ _pytest \ assertion \ rewrite.py”,第274行以添加打印语句以查看它正在尝试写入什么。(您还可以使用类似procmon的工具来监视Python并查看它正在做什么?)。 - nneonneo
问题出在tempfile.py文件中。如果出现访问冲突(例如临时文件名与临时目录相同),则会无限重复。首先,为什么有人将重复计数设置为2 ** 32...即使将循环设置为20次尝试,它仍然失败。其次,tempfile尝试在每个已安装的第三方包的每个__pycache__目录中创建文件,例如coloramatoml等等。这是什么鬼?第三,我禁用了访问冲突时的重试,然后pytest在10次运行中有5次失败...现在的问题是如何修复Python核心,并与谁讨论此问题? - Paebbels
6个回答

2
您可以在第一次运行时使用特权(管理员)cmd来运行pytest。对于我来说,问题已经得到解决,现在pytest可以在普通cmd中运行而不会挂起。

1

问题已经解决。

答案似乎是:

  1. 通过Windows应用程序和功能卸载Python
  2. 删除c:\program files\python38目录
  3. 删除..\AppData\Roaming\Python目录
  4. 重新安装

不确定“根”问题是什么,但完全清除Python解决了它。 请注意,Python Windows安装程序未删除足够的Python内容。

感谢@nneonneo让我思考正确的方向。


0

我有类似的问题

在安装pytest-covallure-pytest后,pytest会卡住

如果我卸载它们,一切都恢复正常

我尝试安装不同版本的pytest,但没有帮助

但是,我尝试了你提供的方法(卸载Python,删除目录),它起作用了,太棒了!

但是,当我再次安装allure-pytest时,它又失败了。

最后,我发现,在特权cmd窗口中无法安装软件包

如果我在特权cmd窗口中安装它们,它会导致pytest卡住!

因此,如果有人遇到我的问题,可以尝试我的方法。


这不是一个答案 - 这可能更适合作为评论。 - tilde
2
移除 pytest-cov 对我也起了作用。 - Max Schmitt

0

使用Ubuntu 18.04/20.04 + Python 3.8.2

我的测试在显示结果后以类似的方式挂起。

删除pytest-cov没有帮助。

有帮助的是从Python 3.8.2升级到更新版本(如3.8.12


0
在我的情况下,JupyterLab 的依赖包 anyio 导致了这个问题。它是以管理员权限安装的。我将其卸载并重新以用户级别安装,pytest 又可以正常工作了。

我刚遇到了同样的问题。难以置信,我从来没有想到这是由于某个不相关的包引起的。 - Wenzel Jakob

0

我遇到了同样的问题。在我的情况下,我的IDE没有加载正确的conda环境。因此,请确保您正在使用正确的应用程序环境,并使用conda activate myEnv在必要时进行更改。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接