How do I import scikit-learn in a jupyter notebook? - python

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.

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).

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)

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.

First activate the finds environment, second launch jupyter notebook, third import sklearn. Inside jupyter notebook.

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!

Related

Why do I get an MKL error when running my python script on MacOS

I have a python script that was converted from an .ipynb notebook from Google Colab that I'm trying to run natively on my Mac running Big Sur.
When I try to run the script using python scriptname.py it gives me the following error:
NTEL MKL ERROR: dlopen(/Users/MyUser/opt/anaconda3/lib/libmkl_core.dylib, 9): image not found.
Intel MKL FATAL ERROR: Cannot load libmkl_core.dylib.
It occurs immediately after I try to import pandas.
I tried conda update numpy as suggested here but the problem persists.
I wondered if I didn't have pandas installed but I did pip install pandas and I got Requirement already satisfied for all 6 packages.
Any help would be appreciated.
Try following, to make sure it's not something with your conda stuff.
> python3 -m venv venv-38
> source venv-38/bin/activate
(venv-38) > pip3 install pandas
...
...
(venv-38) > python
Python 3.8.1 (v3.8.1:1b293b6006, Dec 18 2019, 14:08:53)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>> quit()
** Update **
You can easily reuse this environment in all future terminal sessions. Let's say you have this venv-38 inside $HOME. All you have to do is to open terminal session, and then
> source $HOME/venv-38/bin/activate
since now, your Python environment will be used as if it was installed inside venv-38 directory.
I finally found a solution for this. numpy also gave me the same problem so I uninstalled it pip uninstall numpy and reinstalled it pip install numpy.
I suppose I'll have to do this with all packages as I use them, but perhaps pyenv will provide a useful solution to my problem if. ever get it working.

Unable to load matplotlib in Jupyter Notebook

I'm Unable to load matplotlib in Jupyter Notebook but woking fine in python command line shell,
Is there anything I need to configure to make it working?
Following is the error I'm getting in Jupyter Notebook
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-99ba79ecbbfb> in <module>()
----> 1 from matplotlib import pyplot as plt
ImportError: No module named matplotlib
And in command line I can access it like the following:
Python 3.7.3 (default, Mar 27 2019, 23:47:09)
[Clang 10.0.0 (clang-1000.10.44.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from matplotlib import pyplot as plt
>>>
Seems like it's working now I ran the following command as specified here
python3 -m pip install ipykernel
python3 -m ipykernel install --user
Thanks.
One way is to check if you installed matplotlib using pip3 (if you used pip3 to install jupyter notebook, which looks like is your case).
Another way is to add the path of site-wide packages (where of course matplotlib is installed). In your Jupyter notebook console:
import sys
PATH = '/usr/lib64/python3.7/site-packages/'
sys.path.append(PATH)
Notebook is launched from another virtual python environment.
You can check paths to python interpreters, that run your notebook and interactive shell:
import sys
print(sys.executable)
I'm sure, they will be different.

ImportError for packages in my current environment

I am trying to revive some old IPython notebooks and update them to Python 3. I am trying to get them working first in the old environment and finding an intractable difficulty, getting ImportErrors for packages in my current environment. I am working on an Anaconda Python installation.
I start my notebook by first navigating to the folder of my project:
cd /folder/of/project
and then:
jupyter notebook
I create a new notebook and here is an extract from !conda list inside the notebook and the errors I am getting:
# packages in environment at /anaconda2:
#
...
Delorean 0.6.0 <pip>
...
pylint 1.7.2 py27h718c7e7_0
pymongo 3.4.0 py27_0
pyodbc 4.0.17 py27hc9de18c_0
...
Clearly, the packages of interest, Delorean and pymongo, are in the environment. pymongo was installed via conda natively, but Delorean can only be installed via pip.
[1] import pymongo
ImportError Traceback (most recent call last)
<ipython-input-2-ec8fdd1cd630> in <module>()
----> 1 import pymongo
ImportError: No module named pymongo
[2] from delorean import Delorean
ImportError Traceback (most recent call last)
<ipython-input-3-2b02cbe45080> in <module>()
----> 1 from delorean import Delorean
ImportError: No module named delorean
I have tried this on various virtual environments created using conda as well as in the default environment. I have looked at various questions on StackOverflow and still getting no solution.
In response to a comment, here are the paths to jupyter and python:
$ which jupyter
/anaconda2/bin/jupyter
$ which python
/anaconda2/bin/python
To compound the mystery, trying to import the packages from python shell as suggested by #Sraw, works perfectly.
$ python
Python 2.7.13 |Anaconda custom (x86_64)| (default, Sep 21 2017, 17:38:20)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>> from delorean import Delorean
>>>
The issue therefore is limited to inside the jupyter notebook.
I have resolved this issue. It was created by my trying to juggle different Python 2 and 3 versions and multiple trial virtual environments. I had ended up with an old user-specific anaconda installation and a new anaconda root for all users. The discovery was made by finding that
import sys
sys.path
gave different reports when I ran these from Python shell and from inside the notebook.
I cleared the user-specific anaconda installation, re-installed the latest anaconda version, added the relevant packages, and installed parallel ipython kernels (as described here).
The issue is resolved and serves as a lesson in the importance of keeping the root python install clean and properly using virtual environments.

Tensorflow Not Working from Anaconda on Mac

When it comes to installing Tensorflow, I've tried each of the installation suggestions on this page.
https://www.tensorflow.org/install/install_mac
-Pip + Pip3
-virtualenv
-With Docker
The only installation method that I was unable to apply was Conda. My default environment for Data Science is Spyder launched from Anaconda_Navigator. However, I am unable to get the Conda command to work, in any form, from the command line.
My goal is to get tensor flow working from the iPython console from with in Spyder.
I am trying to run the suggested validation code:
# Python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
Here is the information on my iPython installation:
Python 3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09)
Type "copyright", "credits" or "license" for more information.
IPython 5.3.0 -- An enhanced Interactive Python.
The first line of code throws the following error.
ModuleNotFoundError: No module named 'tensorflow'
When I try to run from Python 2.7 in the command line from terminal, I get:
ImportError: numpy.core.multiarray failed to import
Failed to load the native TensorFlow runtime.
When I try to run it from the terminal command line in Python 3.6.1, I get the following error in regards to the second line of code:
AttributeError: module 'tensorflow' has no attribute 'constant'
I followed the below steps and it worked for me.
Use Conda to create a new virtual environment called "tensorflow" and install the tensorflow in the new conda environment, following the instructions of below link: https://www.tensorflow.org/install/install_mac. The section name is "Installing with Anaconda". All these steps are run through a MAC terminal.
Launch the Anaconda Navigator as usual
Switch to the new "tensorflow" environment using the drop-down box at the top. This is important. By default, "root" environment is chosen.
The new "tensorflow" environment does not have spyder installed. Click the "Install" button. You should see the screenshot like below
5. Launch spyder and type the sample tensorflow codes you want to execute.
Good luck.
Try this:
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0-cp27-none-linux_x86_64.whl
sudo pip install --upgrade $TF_BINARY_URL
The first error
ModuleNotFoundError: No module named 'tensorflow'
is related to Anaconda path. Anaconda does't use the PYTHONPATH. Try:
unset PYTHONPATH
source activate anaconda-x.x #your version instead of x.x
python
>>>> import tensorflow as tf
The second error
ImportError: numpy.core.multiarray failed to import
Failed to load the native TensorFlow runtime.
is due numpy version needs of tensorflow, try upgrade numpy.
The third error,
AttributeError: module 'tensorflow' has no attribute 'constant'
may be related to your binary (hint: check if you are using the correct CPU (or GPU) for your OS).
Let me know if something helped you. Good luck! :)

How to solve: ImportError: "No module named 'graphlab'?

With
"source activate graphlab"
in the terminal I can start up graphlab.
I've created it like this: "conda create -n graphlab python=2.7 anaconda", because using virtualenv with Anaconda is untested and not recommended (according to the warning in the terminal, I don't know whether this really is the case.).
After starting up graphlab the terminal shows:
discarding /Users/username/anaconda/bin from PATH
prepending /Users/username/anaconda/envs/graphlab/bin to PATH
But when I want to import graphlab in the Spider IDE it shows the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/username/anaconda/lib/python3.4/site packages/spyderlib/widgets/externalshell/sitecustomize.py", line 580, in runfile
execfile(filename, namespace)
File "/Users/username/anaconda/lib/python3.4/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 48, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "/Users/username/Documents/projectname/pythonfile.py", line 3, in <module>
import graphlab as gl
ImportError: No module named 'graphlab'
How can I solve this? I am totally new to installing these things, so hopefully someone can help me with an extensive step by step explanation.
The Dato Graphlab Create installer did not actually install graphlab on my Mac (El Capitan). I did the following (Anaconda is installed) in a terminal window:
% pip install graphlab-create
That subsequently installed Graphlab Create. You can then easily verify:
% python
Python 2.7.10 |Continuum Analytics, Inc.| (default, Sep 15 2015, 14:29:08)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import graphlab
>>>
I've noticed that occasionally, Python will forget that Graphlab Create is installed. A repeat of the above 'pip' command will cause it to remember.
python anaconda graphlab
Open Anaconda command prompt. Run the following command:
pip install graphlab-create
The Spider IDE seems to be configured to use Python3.4 by default. That has to be changed to 2.7. This is from the error message on the issue.
There seems to be an issue with the Spider IDE.
Try importing from the command line interpreter (Anaconda version) and it should work fine.
Got the same error message, although I for sure had installed GraphLab on an environment with conda on Windows. I solved it with kernels for different environments.
Do the following on command line:
activate myenv
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
replace myenv in the code above with the name of the (conda-)environment in which graphlab is installed
Source (kernels for different environments): https://ipython.readthedocs.org/en/stable/install/kernel_install.html
Oh and to be sure that you actually had installed graphlab correct in your environment in the first place, you can test this by doing in command line:
Change environment:
activate nameofyourenvironment
Start Python:
python
Check in Python if GraphLab is found:
import graphlab
(if you don't get an error message, you did install graphlab correct in that particular environment)

Categories

Resources