简体   繁体   English

ImportError: 无法导入 openpyxl

[英]ImportError: can't import openpyxl

I'm working in Spyder with the Anaconda interpreter.我正在使用 Anaconda 解释器在 Spyder 工作。 Python version is 3.10.5. Python 版本是 3.10.5。 Tried installing openpyxl with both the Windows CMD and the Anaconda prompt and they are both telling me openpyxl is installed.尝试使用 Windows CMD 和 Anaconda 提示符安装 openpyxl,它们都告诉我 openpyxl 已安装。 But Spyder stills throws the same errors:但是 Spyder 仍然会抛出相同的错误:

ModuleNotFoundError: No module named 'openpyxl'

ImportError: Missing optional dependency 'openpyxl'.  Use pip or conda to install openpyxl.

There is no other Python version installed on my PC.我的 PC 上没有安装其他 Python 版本。 What should I do?我应该怎么办?

What is most likely happening is that Spyder is not using the correct Python interpreter.最有可能发生的是 Spyder 没有使用正确的 Python 解释器。 If you're launching Spyder from the Anaconda navigator, it doesn't take the env you launched it from in all cases.如果您从 Anaconda 导航器启动 Spyder,它不会在所有情况下都使用您启动它的环境。

Is the conda env where you installed openpyxl the one listed at the bottom of the Spyder window?您安装 openpyxl 的 conda env 是否是 Spyder 窗口底部列出的环境?

底部 Spyder 面板

If not, click it and chose the path to your python.exe file in the Anaconda env (example below).如果没有,请单击它并在 Anaconda 环境中选择您的 python.exe 文件的路径(下面的示例)。

Python 解释器的设置

So, I am not sure this technically qualifies for an answer but my problem was solved.所以,我不确定这在技术上是否有资格获得答案,但我的问题已经解决。

I solved the issue by uninstalling Anaconda and Spyder.我通过卸载 Anaconda 和 Spyder 解决了这个问题。 I had installed Spyder first, then Anaconda which probably messed up the environments.我先安装了 Spyder,然后是 Anaconda,这可能会弄乱环境。 The morale of the story: only install Anaconda and let it set up everything for you automatically.故事的精神:只安装 Anaconda 并让它自动为您设置一切。

Try below commands to reinstall openpyxl in both Anaconda & system.尝试以下命令在 Anaconda 和系统中重新安装 openpyxl。

pip uninstall openpyxl

python -m pip install openpyxl

or或者

python3 -m pip3 install openpyxl

verify by验证

pip3 show openpyxl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM