Python3.11运行pytest报错KeyError:'__spec__'及AttributeError求助
问题解决建议
问题背景
在PyCharm及命令行中执行Python仓库的pytest测试时均出现相同错误,当前使用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 3.11和3.12.2,担心安装旧版本破坏默认环境,且不确定是否有效
- 尝试更新py包至1.10版本,引发其他包连锁更新需求
解决建议
1. 使用虚拟环境隔离旧版本Python
无需修改系统默认环境,通过工具创建独立虚拟环境安装兼容的Python版本:
- 安装pyenv(macOS执行
brew install pyenv,其他系统参考对应安装方式) - 安装pytest 6.2.5支持的最高Python版本3.10.x:
pyenv install 3.10.12 - 在仓库目录下设置局部Python版本:
pyenv local 3.10.12 - 创建并激活虚拟环境:
python -m venv venv,然后执行source venv/bin/activate(macOS/Linux)或venv\Scripts\activate(Windows) - 重新安装所有依赖:
pip install -r requirements.txt,之后再执行测试
2. 适配现有Python版本的依赖调整
若不想安装旧Python,可尝试调整依赖版本:
- 先卸载现有py和pytest:
pip uninstall py pytest - 安装兼容Python 3.11的pytest(至少7.0以上)及对应py包:
pip install pytest>=7.0 py>=1.11.0 - 检查其他依赖兼容性,若出现冲突,逐一升级冲突包到支持Python 3.11的版本(如Flask、Jinja2等)
3. 临时绕过apipkg的__spec__问题
若上述方法暂时无法实施,可临时修改apipkg.py文件(仅作临时测试,不推荐长期使用):
- 找到路径
/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/py/_vendored_packages/apipkg.py - 在
__makeattr方法中,捕获AttributeError时,若name为__spec__,返回None或空对象
内容的提问来源于stack exchange,提问作者Shisui
相关产品推荐
相关产品推荐

