Python3在CentOS7中的使用问题汇总

本地环境

CentOS7、Python3.7.5

[root@localhost]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

[root@localhost]# python -V
Python 3.7.5

01.Could not import the lzma module

  • 问题描述:导入pandas模块提示“Could not import the lzma module”
[root@localhost]# python
Python 3.7.5 (default, Nov  9 2022, 18:06:54) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
/usr/local/python3/lib/python3.7/site-packages/pandas/compat/__init__.py:120: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
  warnings.warn(msg)
  • 原因分析:Python 没有安装好,./configuremake 的时候出现了错误提示,被忽略了
Python build finished successfully!
The necessary bits to build these optional modules were not found:
_dbm                  _gdbm                 _lzma              nis
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
  • 处理方法:如果是后面才发现的,重新安装太费事了,可以采用如下方法
# <1> 安装依赖包
[root@localhost]# yum -y install xz-devel python-backports-lzma
[root@localhost]# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple backports.lzma

# <2> 编辑lzma.py文件
[root@localhost]# cp /usr/local/python3/lib/python3.7/lzma.py{,.bak}
[root@localhost]# vi /usr/local/python3/lib/python3.7/lzma.py
# 找打如下两行
from _lzma import *
from _lzma import _encode_filter_properties, _decode_filter_properties
# 更换为如下
try:
    from _lzma import *
    from _lzma import _encode_filter_properties, _decode_filter_properties
except ImportError:
    from backports.lzma import *
    from backports.lzma import _encode_filter_properties, _decode_filter_properties

02.xlrd.biffh.XLRDError: Excel xlsx file; not supported

  • 问题描述:Python库xlrd中的xlrd.open_workbook()函数读取xlsx表格文件时出错
temp_df = pd.read_excel(BytesIO(r.content))
File "/usr/local/python3/lib/python3.7/site-packages/xlrd/__init__.py", line 170, in open_workbook
raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')
xlrd.biffh.XLRDError: Excel xlsx file; not supported
  • 原因分析:xlrd 版本问题,2.x版本下有此问题(why?)需要换成1.2.0版本
pip show xlrd
  • 处理方法:换一个1.2.0版本
pip uninstall xlrd
pip install xlrd==1.2.0

xx.模板

  • 问题描述
  • 原因分析
  • 处理方法

posted on 2022-11-18 10:30  これから  阅读(39)  评论(0)    收藏  举报

导航