Python 3.9环境下安装pandas报错及关联botocore问题求助
问题描述
- 升级Python至3.9后,botocore报错:
'xml.etree.ElementTree.Element' object has no attribute 'getchildren',错误位于botocore/parsers.py第434行_replace_nodes方法 - 调整botocore版本后,出现pandas安装错误,栈跟踪如下:
pandas/_libs/reduction.pyx:592:15: Assignment to a read-only property
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in
main()
File "/usr/local/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
File "/usr/local/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
return hook(config_settings)
File "/tmp/pip-build-env-wx42dtie/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 341, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=['wheel'])
File "/tmp/pip-build-env-wx42dtie/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 323, in _get_build_requires
self.run_setup()
File "/tmp/pip-build-env-wx42dtie/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 487, in run_setup
super(_BuildMetaLegacyBackend,
File "/tmp/pip-build-env-wx42dtie/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 338, in run_setup
exec(code, locals())
File "", line 757, in
File "", line 542, in maybe_cythonize
File "/tmp/pip-build-env-wx42dtie/overlay/lib/python3.9/site-packages/Cython/Build/Dependencies.py", line 1134, in cythonize
cythonize_one(*args)
File "/tmp/pip-build-env-wx42dtie/overlay/lib/python3.9/site-packages/Cython/Build/Dependencies.py", line 1301, in cythonize_one
raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: pandas/_libs/reduction.pyx
解决方案
1. 修复botocore的getchildren报错
getchildren()方法在Python 3.9中已被移除,替代方案是直接迭代Element对象本身。推荐两种解决方式:
- 指定兼容版本(优先):安装botocore 1.20.0及以上版本(该版本已适配Python 3.9,移除了
getchildren()调用),执行命令:pip install botocore>=1.20.0 - 临时修改源码(不推荐):找到
botocore/parsers.py第434行,将element.getchildren()替换为list(element),保存后重启服务。
2. 修复pandas安装的CompileError
该错误源于pandas版本与Python 3.9不兼容,或编译环境缺失,解决步骤如下:
- 安装兼容版本:pandas 1.3.0及以上版本支持Python 3.9,执行命令:
pip install pandas>=1.3.0 - 优先使用预编译包:若因缺少编译依赖导致错误,直接安装预编译的wheel包,避免源码编译:
pip install --only-binary :all: pandas>=1.3.0 - 补充编译依赖(如需源码编译):Linux系统下安装编译所需依赖:
- Debian/Ubuntu:
sudo apt-get install gcc python3.9-dev libatlas-base-dev - RHEL/CentOS:
sudo yum install gcc python39-devel atlas-devel
- Debian/Ubuntu:
内容的提问来源于stack exchange,提问作者Vishal Singh

