ModuleNotFoundError: No module named 'openpyxl'

一、问题现象

执行python脚本的时候报错:

ModuleNotFoundError: No module named 'openpyxl'

其实我已经安装过openpyxl了;

# 安装命令
pip install openpyxl
# 查找已安装的模块
pip list |grep openpyxl
DEPRECATION: The default format will switch to columns in the future. 
You can use --format=(legacy|columns) (or define a format=(legacy|columns) 
in your pip.conf under the [list] section) to disable this warning.
openpyxl (2.6.4)

但是为什么还是会报错呢?

二、问题原因

  由于python有多个版本:python2和python3,并且两个版本的模块还不能共用,因此有不同的pip版本:pip和pip3,分别用于安装python2和python3的模块;因此需要使用不同的pip版本安装python需要的模块;
python2的模块安装命令:

pip install openpyxl

python3的模块安装命令:

pip3 install openpyxl

如果pip3没有安装,则需要安装pip3:

sudo apt install python3-pip

最终问题就解决了:

pip list |grep openpyxl
DEPRECATION: The default format will switch to columns in the future. 
You can use --format=(legacy|columns) (or define a format=(legacy|columns) 
in your pip.conf under the [list] section) to disable this warning.
openpyxl (2.6.4)

pip3 list |grep openpyxl
DEPRECATION: The default format will switch to columns in the future. 
You can use --format=(legacy|columns) (or define a format=(legacy|columns) 
in your pip.conf under the [list] section) to disable this warning.
openpyxl (3.1.2)
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐