I want to import sklearn but there is no module apparently:
ModuleNotFoundError: No module named 'sklearn'
I am using Anaconda and Python 3.6.1
; I have checked everywhere but still can't find answers.
When I use the command:
conda install scikit-learn
should this not just work?
Where does anaconda install the package?
I was checking the frameworks in my python library and there was nothing about sklearn only numpy and scipy.
Please help, I am new to using python packages especially via anaconda.
You can just use pip
for installing packages, even when you are using anaconda:
pip install -U scikit-learn scipy matplotlib
This should work for installing the package.
And for Python 3.x just use pip3
:
pip3 install -U scikit-learn scipy matplotlib
install these ==>> pip install -U scikit-learn scipy matplotlib if still getting the same error then , make sure that your imoprted statment should be correct. i made the mistike while writing ensemble so ,(check spelling) its should be >>> from sklearn.ensemble import RandomForestClassifier
SOLVED:
The above did not help. Then I simply installed sklearn from within Jypyter-lab, even though sklearn 0.0 shows in 'pip list':
!pip install sklearn
import sklearn
What I learned later is that pip installs, in my case, packages in a different folder than Jupyter. This can be seen by executing:
import sys
print(sys.path)
Once from within Jupyter_lab notebook, and once from the command line using 'py notebook.py'.
In my case Jupyter list of paths where subfolders of 'anaconda' whereas Python list where subfolders of c:\users[username]...
This happened to me, I tried all the possible solutions with no luck!
Finaly I realized that the problem was with Jupyter notebook environment, not with sklearn!
I solved the problem by re-installing Jupyter at the same environment as sklearn
the command is: conda install -c anaconda ipython
. Done...
I did the following:
import sys
!{sys.executable} -m pip install sklearn
Brief Introduction
When using Anaconda, one needs to be aware of the environment that one is working.
Then, in Anaconda Prompt (base) one needs to use the following code:
conda $command -n $ENVIRONMENT_NAME $IDE/package/module
$command - Command that I intend to use (consult documentation for general commands)
$ENVIRONMENT NAME - The name of your environment (if one is working in the root,
conda $command $IDE/package/module
is enough)
$IDE/package/module - The name of the IDE or package or module
Solution
If one wants to install it in the root and one follows the requirements - (Python (>= 2.7 or >= 3.4), NumPy (>= 1.8.2), SciPy (>= 0.13.3).) - the following will solve the problem:
conda install scikit-learn
Let's say that one is working in the environment with the name ML.
Then the following will solve one's problem:
conda install -n ML scikit-learn
Note: If one needs to install/update packages, the logic is the same as mentioned in the introduction. If you need more information on Anaconda Packages, check the documentation.
If the above doesn't work, on Anaconda Prompt one can also use pip (here's how to pip install scikit-learn) so the following may help
pip install scikit-learn
I've tried a lot of things but finally, including uninstall with the automated tools. So, I've uninstalled manually scikit-learn.
sudo rm -R /home/ubuntu/.local/lib/python3.6/site-packages/sklearn
sudo rm -R /home/ubuntu/.local/lib/python3.6/site-packages/scikit_learn-0.20.0-py3.6.egg-info
And re-install using pip
sudo pip3.6 install -U scikit-learn
Hope that can help someone else!
The other name of sklearn in anaconda is scikit-learn. simply open your anaconda navigator, go to the environments, select your environment, for example tensorflow or whatever you want to work with, search for scikit_learn in the list of uninstalled packages, apply it and then you can import sklearn in your jupyter.
I had the same issue as the author, and ran into the issue with and without Anaconda and regardless of Python version. Everyone's environment is different, but after resolving it for myself I think that in some cases it may be due to having multiple version of Python installed. Each installed Python version has its own \Lib\site-packages\ folder which can contain a unique set of modules for that Python version, and where the IDE looks into folder path that doesn't have scikit-learn in it.
One way to try solve the issue: you might clear your system of all other Python versions and their cached/temp files/system variables, and then only have one version of Python installed anywhere. Then install the dependencies Numpy and Scipy, and finally Scikit-learn.
More detailed steps:
As some others have suggested, the key is making sure your environment is set up correctly where everything points to the correct library folder on your computer where the Sklearn package is located. There are a few ways this can be resolved. My approach was more drastic, but it turns out that I had a very messy Python setup on my system so I had to start fresh.
If you are using Ubuntu 18.04 or higher with python3.xxx then try this command
$ sudo apt install python3-sklearn
then try your command. hope it will work
Conda and pip install scikit-learn under ~/anaconda3/envs/$ENV/lib/python3.7/site-packages, however Jupyter notebook looks for the package under ~/anaconda3/lib/python3.7/site-packages.
Therefore, even when the environment is specified to conda, it does not work.
conda install -n $ENV scikit-learn # Does not work
pip 3 install the package under ~/anaconda3/lib/python3.7/site-packages.
After pip3, in a Jupyter notebook.
import sklearn
sklearn.__file__
~/anaconda3/lib/python3.7/site-packages/sklearn/init.py'
Source: Stackoverflow.com