问题
在pip安装时,出现
Collecting matplotlib==3.3.3 (from -r requirements.txt (line 20))
Downloading matplotlib-3.3.3.tar.gz (37.9 MB)
---------------------------------------- 37.9/37.9 MB 727.1 kB/s eta 0:00:00
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [16 lines of output]
Traceback (most recent call last):
File "<string>", line 2, in <module>
File "<pip-setuptools-caller>", line 34, in <module>
File "C:\Users\lutao\AppData\Local\Temp\pip-install-epd8fs5d\matplotlib_6c900e11b6454be496de6443b6d0a51e\setup.py", line 52, in <module>
__version__ = versioneer.get_version()
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lutao\AppData\Local\Temp\pip-install-epd8fs5d\matplotlib_6c900e11b6454be496de6443b6d0a51e\versioneer.py", line 1410, in get_version
return get_versions()["version"]
^^^^^^^^^^^^^^
File "C:\Users\lutao\AppData\Local\Temp\pip-install-epd8fs5d\matplotlib_6c900e11b6454be496de6443b6d0a51e\versioneer.py", line 1344, in get_versions
cfg = get_config_from_root(root)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lutao\AppData\Local\Temp\pip-install-epd8fs5d\matplotlib_6c900e11b6454be496de6443b6d0a51e\versioneer.py", line 401, in get_config_from_root
parser = configparser.SafeConfigParser()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'configparser' has no attribute 'SafeConfigParser'. Did you mean: 'RawConfigParser'?
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
原因
安装matplotlib版本3.3.3时,setup.py脚本执行失败。错误信息提示在versioneer.py文件中,configparser模块没有SafeConfigParser属性。这是因为在Python 3中,SafeConfigParser已经被重命名为ConfigParser。
Versioneer 与现代 Python 不兼容,需要更新代码。
解决
升级matplotlib到一个更新的版本:较新的版本可能已经修复了这个兼容性问题。尝试安装最新版本的matplotlib,或者至少是一个已知兼容Python 3的版本。
修改requirements.txt:在requirements.txt文件中更新matplotlib的版本。例如,如果您使用的是Python 3,您可以尝试将matplotlib版本改为3.4或更高
手动修复问题:如果您由于某些原因需要坚持使用matplotlib的3.3.3版本,并且不能升级,您可以尝试编辑出错的versioneer.py文件,将所有的SafeConfigParser替换为ConfigParser。但是,请注意,这种方法可能会引入其他兼容性问题,且不是推荐的做法。
ref
https://github.com/pydata/pandas-datareader/issues/969