Pytest KeyError: '__spec__' 在处理上述异常的过程中,又发生了一个异常: AttributeError: __spec__ Empty suite

问题描述 投票:0回答:1

我正在尝试在 pycharm 中的 python 存储库上执行一些 pytest 测试。我尝试过从命令行单独执行测试,但出现了相同的错误。

我正在使用 python 并且我的 python 解释器设置为 3.11

/usr/local/bin/python3.11 /Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/_jb_pytest_runner.py --path /Users/shannonj/Downloads/requests-master/tests 
Testing started at 1:38 AM ...
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/py/_vendored_packages/apipkg.py", line 141, in __makeattr
    modpath, attrname = self.__map__[name]
                        ~~~~~~~~~~~~^^^^^^
KeyError: '__spec__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm/_jb_pytest_runner.py", line 5, in <module>
    import pytest
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pytest/__init__.py", line 5, in <module>
    from _pytest.assertion import register_assert_rewrite
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/_pytest/assertion/__init__.py", line 9, in <module>
    from _pytest.assertion import rewrite
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/_pytest/assertion/rewrite.py", line 34, in <module>
    from _pytest.assertion import util
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/_pytest/assertion/util.py", line 13, in <module>
    import _pytest._code
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/_pytest/_code/__init__.py", line 2, in <module>
    from .code import Code
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/_pytest/_code/code.py", line 54, in <module>
    class Code:
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/_pytest/_code/code.py", line 81, in Code
    def path(self) -> Union[py.path.local, str]:
                            ^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/py/_vendored_packages/apipkg.py", line 148, in __makeattr
    result = importobj(modpath, attrname)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/py/_vendored_packages/apipkg.py", line 69, in importobj
    module = __import__(modpath, None, None, ['__doc__'])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1136, in _find_and_load_unlocked
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/py/_vendored_packages/apipkg.py", line 146, in __makeattr
    raise AttributeError(name)
AttributeError: __spec__

Process finished with exit code 1

Empty suite

回购的要求如下:

alabaster==0.7.7
Babel==2.2.0
coverage==5.0.3
decorator==4.0.9
docutils==0.12
Flask==0.10.1
httpbin==0.5.0
itsdangerous==0.24
Jinja2==2.10.3
MarkupSafe==1.1.0
pluggy==0.13.0
py==1.8.2
Pygments==2.1.1
PySocks==1.7.0
pytest-cov==2.4.0
pytest-httpbin==0.2.0
pytest-mock==0.11.0
pytest==6.2.5
pytz==2015.7
six==1.10.0
snowballstemmer==1.2.1
sphinx-rtd-theme==0.1.9
Sphinx==1.3.5
urllib3==1.26.7
Werkzeug==0.11.4
wheel==0.29.0

这看起来确实已经过时了。所以我随意猜测也许我需要旧版本的Python?但我已经通过 python.org 的官方 mac 安装程序安装了 3.11 和 3.12.2。较旧的过时版本的 python 没有如此简单的安装选项,尽管我想我可以使用brew?我只是不想弄乱我的默认 python 版本,而且我也不知道它是否能工作。

我尝试过更新一些软件包,例如将 py 更新到最新版本 1.10,但这会引发一系列需要更新其他软件包的问题。

任何见解将不胜感激

python python-3.x pycharm pytest
1个回答
0
投票

我在尝试使用

pytest
运行测试时遇到了同样的问题。我什至无法运行
pytest --help
并且收到此错误:

Traceback (most recent call last):
  File "/Users/amer/.pyenv/versions/3.12.1/envs/learnpythonprogramming-ch10/lib/python3.12/site-packages/py/_vendored_packages/apipkg/__init__.py", line 145, in __makeattr
    modpath, attrname = self.__map__[name]
                        ~~~~~~~~~~~~^^^^^^
KeyError: '__spec__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/amer/.pyenv/versions/learnpythonprogramming-ch10/bin/pytest", line 5, in <module>
    from pytest import console_main
  File "/Users/amer/.pyenv/versions/3.12.1/envs/learnpythonprogramming-ch10/lib/python3.12/site-packages/pytest/__init__.py", line 8, in <module>
    from _pytest._code import ExceptionInfo
  File "/Users/amer/.pyenv/versions/3.12.1/envs/learnpythonprogramming-ch10/lib/python3.12/site-packages/_pytest/_code/__init__.py", line 5, in <module>
    from .code import Code
  File "/Users/amer/.pyenv/versions/3.12.1/envs/learnpythonprogramming-ch10/lib/python3.12/site-packages/_pytest/_code/code.py", line 45, in <module>
    from _pytest._io import TerminalWriter
  File "/Users/amer/.pyenv/versions/3.12.1/envs/learnpythonprogramming-ch10/lib/python3.12/site-packages/_pytest/_io/__init__.py", line 3, in <module>
    from .terminalwriter import get_terminal_width
  File "/Users/amer/.pyenv/versions/3.12.1/envs/learnpythonprogramming-ch10/lib/python3.12/site-packages/_pytest/_io/terminalwriter.py", line 14, in <module>
    from ..compat import assert_never
  File "/Users/amer/.pyenv/versions/3.12.1/envs/learnpythonprogramming-ch10/lib/python3.12/site-packages/_pytest/compat.py", line 28, in <module>
    LEGACY_PATH = py.path. local
                  ^^^^^^^^^^^^^^
  File "/Users/amer/.pyenv/versions/3.12.1/envs/learnpythonprogramming-ch10/lib/python3.12/site-packages/py/_vendored_packages/apipkg/__init__.py", line 152, in __makeattr
    result = importobj(modpath, attrname)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/amer/.pyenv/versions/3.12.1/envs/learnpythonprogramming-ch10/lib/python3.12/site-packages/py/_vendored_packages/apipkg/__init__.py", line 72, in importobj
    module = __import__(modpath, None, None, ['__doc__'])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1320, in _find_and_load_unlocked
  File "/Users/amer/.pyenv/versions/3.12.1/envs/learnpythonprogramming-ch10/lib/python3.12/site-packages/py/_vendored_packages/apipkg/__init__.py", line 150, in __makeattr
    raise AttributeError(name)
AttributeError: __spec__

注意:我正在运行基于Python 3.12.1的virtualenv

learnpythonprogramming-ch10
(使用pyenvpyenv-virtualenv创建)。

我首先确保我已经使用

pytest
命令升级了
pip install --upgrade pytest
pip 包:

...
   Uninstalling pytest-6.2.4
...
Successfully installed pluggy-1.5.0 pytest-8.3.3

然后我使用

pip list
命令验证了 pip 安装的 virtualenv 软件包:

Package     Version
----------- -------
attrs       21.2.0
iniconfig   1.1.1
marshmallow 3.12.1
packaging   20.9
pip         24.2
pluggy      1.5.0
py          1.10.0
pyparsing   2.4.7
pytest      8.3.3
toml        0.10.2

在确认我正在运行最新的

pytest
(当时为8.3.3)并且仍然收到错误后,我开始怀疑固定在
requirements.txt
中的其他依赖项:

# requirements.txt

#
# This file is autogenerated by pip-compile
# To update, run:
#
#    pip-compile requirements.in
#
attrs==21.2.0
    # via pytest
iniconfig==1.1.1
    # via pytest
marshmallow==3.12.1
    # via -r requirements.in
packaging==20.9
    # via pytest
pluggy==0.13.1
    # via pytest
py==1.10.0
    # via pytest
pyparsing==2.4.7
    # via packaging
pytest==6.2.4
    # via -r requirements.in
toml==0.10.2
    # via pytest

我用

pip list --outdated
命令进行了验证,其中列出了这些:

Package     Version Latest Type
----------- ------- ------ -----
attrs       21.2.0  24.2.0 wheel
iniconfig   1.1.1   2.0.0  wheel
marshmallow 3.12.1  3.22.0 wheel
packaging   20.9    24.1   wheel
py          1.10.0  1.11.0 wheel
pyparsing   2.4.7   3.1.4  wheel

然后我用命令

pip install --upgrade attrs iniconfig marshmallow packaging py pyparsing
更新了所有这些(请参阅 pip-upgrade-outdated 了解 CLI 项目,该项目可以自动升级所有过时的软件包,尽管它没有得到太多维护)。

之后

pytest --help
pytest
就没有抛出错误了。我不确定哪个特定的过时软件包导致了我的案例(或多个软件包)的问题,并且由于原始帖子中缺少此数据,我对您的案例更加不确定。但就我而言,问题是
requirements.txt
是由代码作者生成的(来自
requirements.in
并使用
pip-compile
工具),并且它包含较旧的
pytest
以及旧版本
pytest
所需的所有其他依赖项(
attrs
iniconfig
packaging
py
pyparsing
)。当我只更新
pytest
时,这些依赖项仍然存在,而
pytest
不喜欢这样。在您的情况下,您可能会遇到类似的情况,其中
pytest
与您环境中的某些其他软件包不兼容,因此如果您想缩小范围,或者只是更新所有过时的软件包,则需要找出哪个软件包,就像我一样做到了。

希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.