Reputation: 795
I created a fresh conda environment for using scikit-learn and used
conda install <package>
to install scikit-learn
, jupyter
, pandas
, etc. for compatible dependencies..
I checked if sklearn
was working after loading the environment:
$python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sklearn
>>>
Since import
command didn't throw errors, sklearn
is ready for use. However, I am getting a ModuleNotFoundError
while trying to import it in a jupyter notebook, that I am running from the same environment.
import sklearn
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-b7c74cbf5af0> in <module>()
----> 1 import sklearn
ModuleNotFoundError: No module named 'sklearn'
I was able to import numpy
and pandas
in the same notebook without any errors.
Please help me understand the problem and how to troubleshoot it.
Upvotes: 2
Views: 63443
Reputation: 11
I have also found the same issue
ModuleNotFoundError: No module named 'sklearn'
but after searching I found its best solution.
You should also try the following steps:
Step 1:
open "cmd"
Step 2:
write "pip install notebook"
Step 3:
After installation of notebook, write "jupyter notebook " in cmd.
Tell me if you got your solution thank you!
Upvotes: 1
Reputation: 1163
First activate the finds environment, second launch jupyter notebook, third import sklearn. Inside jupyter notebook.
Upvotes: 0
Reputation: 1
Check your path for Python and Jupyter:
import sys
sys.path
You may find different results from Python and Jupyter. This can be fixed temporarily by appending this line of code if you are using macos:
sys.path.append('/Users/**your_user_name**/anaconda3/lib/python3.7/site-packages')
If you want to solve this permanently, you can create an iPython profile and fix it there.
Upvotes: 0
Reputation: 802
Make sure that your jupyter notebook is finding the same version of python as your terminal, otherwise installing modules with conda install
in your terminal won't show up in your notebook. Do
import sys
print(sys.version)
in your notebook and in your terminal. If they do not match up, then add your terminal's python version to your notebook:
conda install nb_conda_kernels
conda install ipykernel
and then in the notebook switch to the kernel you just installed (kernel -> change kernel)
Upvotes: 0
Reputation: 795
Best practice: Install everything via conda or pip3, as mentioned in this answer.
If that didn't work, check the system paths in jupyter notebook:
import sys
sys.path
and the system executable:
sys.executable
These must correspond to the python in your current loaded environment.
For me, the issue was with the jupyter Notebook's kernel. See the kernel specifications in kernel.json
file in the path. You can find the directory for this file from jupyter kernelspec list
. I manually changed the python path to the python in my environment (this is a bad idea, but it worked).
Upvotes: 5