How to fix 'ImportError: No module named openpyxl'?
Asked Answered
A

3

7

I am using Python 2.7 and am trying to run a program with openpyxl to work with xlsx files.

In the first line of code:

from openpyxl import Workbook

I get the following error when I run the program from the CMD: ImportError: No module named openpyxl

I used pip to install version 2.6.0, and if I try to install it again through pip it just says I have the latest version.

Unlike any other question like this that I found here, I am using an IDE called PyCharm which has an option of running the program I am currently editing inside PyCharm, and PyCharm actually recognizes openpyxl and runs great, so why won't it work in the CMD?

Antefix answered 12/2, 2019 at 17:52 Comment(2)
Question: does "import openpyxl" work from a command prompt?Giorgio
Possible duplicate of ModuleNotFoundError: No module named 'openpyxl' in python 3.6Crashing
T
3

If you have multiple versions of python installed it might be related to the version of python that's added to your system path.

What I would suggest you do is check your system path and verify that indeed the python/pip you are using from the command line is 2.7. The same thing applies for PyCharm.

If you need help inspecting your system variables on windows: Check this link out!

Thallus answered 12/2, 2019 at 18:34 Comment(0)
C
7

This answer is for those who land in this page without having openpyxl installed.

run pip install openpyxl command in your terminal

Cyclopedia answered 6/11, 2020 at 8:42 Comment(0)
T
3

If you have multiple versions of python installed it might be related to the version of python that's added to your system path.

What I would suggest you do is check your system path and verify that indeed the python/pip you are using from the command line is 2.7. The same thing applies for PyCharm.

If you need help inspecting your system variables on windows: Check this link out!

Thallus answered 12/2, 2019 at 18:34 Comment(0)
B
0

I had the same error running my script in a virtual environment in VS Code. When I hit the "Run Python File" button, the bash automatically runs:

bin/python3 <path to your active script>

The problem was solved by running the same command after removing "bin/":

python3 <path to your active script>
Brewton answered 13/3, 2024 at 19:2 Comment(7)
In case it helps, your issue is likely because you installed openpyxl in your global python instead of your virtualenv. Removing the bin created another issue where you're likely now running your global install instead of your virtualenv python interpreter, which is located in the bin directory on unix-like OSs or in the scripts directory on Windows.Honghonied
Thanks @monokrome. I used pip uninstall openpyxl in my windows cmd prompt. I reran the script with bin/python3 and got the same error.Brewton
You would now need to bin/pip install openpyxl to get it into your virtualenv since you removed it from globalHonghonied
Thanks @monokrome. I have openpyxl in my requirements.txt file. Here's what's happening: (venv) myhostname: root_dir$ bin/pip install -r requirements.txt - next line: bash: bin/pip: No such file or directoryBrewton
@monokrome: What's interesting here is when I perform (venv) myhostname: root_dir$ pip install -r requirements.txt, all packages are installed in the venv lib as expected, and all work perfectly - except for openpyxl.Brewton
Hi @monokrome. I found I had the wrong python interpreter selected in VS Code. Additionally, my venv path to python wasn't automatically recognized. After manually defining the correct path for python3 and selecting it for my interpreter, all import errors went away.Brewton
Good to hear! Have fun :)Honghonied

© 2022 - 2025 — McMap. All rights reserved.