no module named <library_name>
Asked Answered
K

1

2

I am having error importing modules in my jupyter notebook when running it on a mac machine after successfully installing them using:

!pip install <library name>.

I try it on several modules and error persists.

for example, when running this code

!pip install pyenchant

I get this:

Requirement already satisfied: pyenchant in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (3.2.2)

when running :

import enchant 

I am getting an error message:

ModuleNotFoundError: No module named 'pyspellchecker'

then try to run this (as I read this could help)

!python -m pip install -U pyenchant

and get that:

/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named pip

I think it has something to do with python path and bash_profile but I do not have an intuition for it.

I do not know if it is helpful/relevant, but I have Pycharm, python, Visual Studio Code and anconda installed on my machine. I am doubting that the problem comes from misalignment between the common configuration files of these programs.

can you please help?

I realize that many has encounter similar issue but their solutions did not help me.

p.s. the issue disappear when I use conda install , but I would like to use pip as it give me access to larger library options.

Knightly answered 6/12, 2021 at 8:41 Comment(2)
I can't explain it much more clearly than here... https://mcmap.net/q/1174511/-python-pillow-or-pil-not-working-despite-pip-installationWorrell
this didn't help. i have this (Python 3 (ipykernel)) in my jupyter notebook.Knightly
S
1

Based on the error message itself, it appears that some of the code in pyenchant contains a dependency on a package called pyspellchecker, and that package is not properly installed. The developers of pyenchant probably didn't set up the dependency via requires correctly.

You can fix this by running the following commands

%pip install -U pyenchant pyspellchecker

Then restart the Jupyter kernel and try import enchant again.

Note 1:

Unlike the questioner, if someone wants to use it in the terminal, you'll need to remove the % from that command.

pip install -U pyenchant pyspellchecker

Note 2:

If you additionally get an error like ModuleNotFoundError: No module named '<package_name>', running the following command will fix it in more than 70% of cases.

%pip install -U <package_name>

Note 3:

The reason why !python -m pip install -U pyenchant gave incomprehensible problems was that the python environment where Jupyter was installed and the python environment used by the python command were different, and python's environment did not have pip installed.

According to the logs, the environment jupyter was using was /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9, and the environment accessed via the python command was /System/Library/Frameworks/Python.framework/Versions/2.7. Also, pip was not installed in python's environment, which is why you got the error that pip was not installed.

It won't fix the problem, but at least you can use python3.9 instead of python to match the environment Jupyter uses with the one you'll use in the terminal.

Note 4:

I can only speculate as I can't verify this myself, but the reason conda install fixed the problem is that conda already had pyspellchecker installed, or maybe it's just that there are different versions of pyenchant that are installed when using conda and when using pip.

Spriggs answered 21/4, 2024 at 3:58 Comment(3)
Please note that suggesting the use of an exclamation point with pip install is an outdated pattern now. I know your approach added steps to try to insure it went to the right environment; however, the magic install commands added in 2019 do this and are cleaner and more convenient and now pretty much universal as far as I know, now working in JupyterLite and Google Colab. See more about the modern %pip install command here. ...Dictum
<continued> The second paragraph here goes into more details about why the exclamation point may lead to issues. It was such issues that lead to adding the magic install version for pip with the magic install command being the current best practice. (Since this post mentioned Anaconda/conda, I'll add there is a magic %conda install as well for running inside active Jupyter .ipynb files.)Dictum
@Dictum Thanks for the great information! I didn't know about that. I'll apply better solution you suggest.Spriggs

© 2022 - 2025 — McMap. All rights reserved.