[Tutor] openpyxl module not recognized
Mats Wichmann
mats at wichmann.us
Wed Jan 8 17:16:36 EST 2020
On 1/7/20 1:31 PM, Casey McGonigle wrote:
> I've done this -- it gives me the message
>
> Requirement already satisfied: openpyxl in
> /anaconda3/lib/python3.7/site-packages (3.0.2)
>
> anything else that could be causing the issue?
>>> I'm working on a project using pandas DataFrames that I'd like to export
>>> to
>>> excel. I'm using Mac OS Mojave. I understand the way to do this is through
>>> the df.to_excel() function, which requires the openpyxl module.
>>>
>>> As such, I've installed the module (using pip install openpyxl) -- my
>>> terminal tells me it was successfully installed and it shows up in my
>>> finder.
>>>
>>> However, when I run the df.to_excel("excel file") function, I get a
>>> ModuleNotFoundError: No module named 'openpyxl'.
The good news is we know exactly what's wrong. That message always
means a path problem, one of two possibilities:
- the module is not installed. that is, there is no path at all on the
system that would work to import it.
- or, the module is installed, but the python instance you're using
doesn't know the path to it.
You don't want a history lesson, but there was a time when if you had
python, everything was attached to that Python, and it was a little
simpler. These days, odds are pretty good you have more than one, and
ones you have may or may not have several distinct instances
(virtualenvs). The message at the top indicates you have a separate
distribution via Anaconda, so if you've installed a regular Python.org
python, or are running Linux or Mac (which come with a system-provided
version), in which case you have at least two. I'm not an Anaconda user,
but I believe Conda is quite aggressive in setting up virtualenvs.
For any given invocation of Python, here's how you see where it knows to
look:
>>> import sys
>>> print(sys.path)
One persistent problem we're seeing a lot of these days is that
instructions for things say to do
pip install this-wonderful-package
If you have several Pythons installed, how can you know which Python pip
is going to install for? Therefore, it's usually worth modifying that
instruction they have given you and invoke it this way:
python -m pip install this-wonderful-package
then whatever is installed matches the python you invoked that command with.
However... if you're using Anaconda, it has its own installation system
and doesn't install using pip, but you also indicated you used pip to
install it, so at this point I'm pretty confused. Conda should be able
to get things right - but then you have to invoke the same Python as it
does.
This is without even getting into the complication of virtualenvs.
So the challenge is to get things to match. For whichever Python you
used that gave you the failure, try this:
python -m pip list
and see if it lists openpyxl
or invoke it interactively and try the import:
>>> import openpyxl
Hope this gets you at least a little closer to understanding, if not
solving.
>>> UC Berkeley
>>> Class of 2021
Cheers!
-- mats, UC Berkeley Class of Long Ago
(well, okay, 1981)
More information about the Tutor
mailing list