notebook: Import on Jupyter notebook failed where command prompt works.

Hi,

I am new to Python and Anaconda. I installed anaconda and install Scipy. When I try import scipy in the Python in command prompt on the Anaconda prompt, it works fine as below

[Anaconda3] C:\Users\me>python
Python 3.5.1 |Anaconda 4.0.0 (64-bit)| (default, Feb 16 2016, 09:49:46) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy
>>>

But when I use the same in the Jupyter notebook, I am getting -

ImportError                               Traceback (most recent call last)
<ipython-input-3-4dc45f4c0083> in <module>()
----> 1 import scipy

ImportError: No module named 'scipy'

Please let me know how to solve this issue.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 78
  • Comments: 63 (13 by maintainers)

Most upvoted comments

Usually that indicates that the notebook is running with a different Python or in a different environment from Python in the command prompt. Check sys.executable to see which Python it’s running in, and sys.path to see where it’s looking for imports.

try a conda install ipykernel --name Python3, python -m ipykernel install. That will likely fix the notebook can see your Python 3 kernel. Then create a new >Python3 notebook from the dashboard.

$ /Users/abc/anaconda/bin/python -m pip install ipykernel
$ /Users/abc/anaconda/bin/python -m  ipykernel install

Hi, same issue here. Another approach is to install jupyter lab (or jupyter notebook) with “conda install -c conda-forge jupyterlab” after the environment’s activation; I mean, inside the environment, so that the jupyter lab runs on this environment and not on root. May be it’s not so much elegant, but is another way and works.

You can manipulate sys.path inside Python, e.g.

sys.path.remove('/path/you/dont/want')
sys.path.append('/path/you/do/want')

It can also be affected by the $PYTHONPATH environment variable when Jupyter is started.

To install package in jupyter notebook use following instructions: https://www.datadivein.com/2019/01/how-to-install-python-package-in.html

Usually that indicates that the notebook is running with a different Python or in a different environment from Python in the command prompt. Check sys.executable to see which Python it’s running in, and sys.path to see where it’s looking for imports.

When I switch to Python 3 kernel, it still shows /usr/bin/python, which is python2.

So I had to run the following to make everything work

python2 -m pip install ipykernel python2 -m ipykernel install --user

python3 -m pip install ipykernel python3 -m ipykernel install --user

@nikhilrsharma I create a virtual environment in anaconda, then I solve this problem. https://uoa-eresearch.github.io/eresearch-cookbook/recipe/2014/11/20/conda/

After installing the virtual environment, every time when you open the terminal: you should command:

$ source activate _yourEnvName_

then run the jupyter notebook on your virtual environment. command: $ jupyter notebook

If you want to pip install any package, you should install 2 times: the first one is in the terminal: $ pip install something the second time is to enter the virtual environment: $ source activate _yourEnvName_ and $ pip install something

solved it, in my case

conda install ipykernel --name {envname}
python -m ipykernel install --prefix=/anaconda3/envs/{envname} --name {envname}

https://ipython.readthedocs.io/en/stable/install/kernel_install.html

Using virtualenv or conda envs, you can make your IPython kernel in one env available to Jupyter in a different env. To do so, run ipykernel install from the kernel’s env, with –prefix pointing to the Jupyter env:

Use --help and look at the informations:

$ /path/to/python -m  ipykernel install --help
usage: ipython-kernel-install [-h] [--user] [--name NAME]
                              [--display-name DISPLAY_NAME]
                              [--profile PROFILE] [--prefix PREFIX]
                              [--sys-prefix]

Install the IPython kernel spec.

optional arguments:
  -h, --help            show this help message and exit
  --user                Install for the current user instead of system-wide
  --name NAME           Specify a name for the kernelspec. This is needed to
                        have multiple IPython kernels at the same time.
  --display-name DISPLAY_NAME
                        Specify the display name for the kernelspec. This is
                        helpful when you have multiple IPython kernels.
  --profile PROFILE     Specify an IPython profile to load. This can be used
                        to create custom versions of the kernel.
  --prefix PREFIX       Specify an install prefix for the kernelspec. This is
                        needed to install into a non-default location, such as
                        a conda/virtual-env.
  --sys-prefix          Install to Python's sys.prefix. Shorthand for
                        --prefix='/Users/bussonniermatthias/anaconda'. For use
                        in conda/virtual-envs.

A --user flag is available. Or, if you know what your doing, use sudo to install with administrator permission.

Hi,

I am new to Python and Anaconda. I installed anaconda and install Scipy. When I try import scipy in the Python in command prompt on the Anaconda prompt, it works fine as below

[Anaconda3] C:\Users\me>python
Python 3.5.1 |Anaconda 4.0.0 (64-bit)| (default, Feb 16 2016, 09:49:46) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy
>>>

But when I use the same in the Jupyter notebook, I am getting -

ImportError                               Traceback (most recent call last)
<ipython-input-3-4dc45f4c0083> in <module>()
----> 1 import scipy

ImportError: No module named 'scipy'

Please let me know how to solve this issue.

This might work for you.

Here is a short snippet that should generally work: import sys !{sys.executable} -m pip install scipy

first use conda install nb_conda_kernels to set jupter notebook’s envirnment second change kernel to choose the environment

Usually that indicates that the notebook is running with a different Python or in a different environment from Python in the command prompt. Check sys.executable to see which Python it’s running in, and sys.path to see where it’s looking for imports.

When I switch to Python 3 kernel, it still shows /usr/bin/python, which is python2.

So I had to run the following to make everything work

python2 -m pip install ipykernel python2 -m ipykernel install --user python3 -m pip install ipykernel python3 -m ipykernel install --user

@pswpswpsw I do that… but do not looks to be my case (by the way the problem is with jupyterhub, not with jupyter, where is working)

sys.executable: /opt/anaconda3/bin/python sys.path: [‘’, ‘/opt/anaconda3/lib/python36.zip’, ‘/opt/anaconda3/lib/python3.6’, ‘/opt/anaconda3/lib/python3.6/lib-dynload’, ‘/opt/anaconda3/lib/python3.6/site-packages’, ‘/opt/anaconda3/lib/python3.6/site-packages/Mako-1.0.7-py3.6.egg’, ‘/opt/anaconda3/lib/python3.6/site-packages/IPython/extensions’, ‘/home/mazzitel/.ipython’]

just upgrade your jupyter notebook :

pip install --upgrade pip pip install jupyter

I was having the same problem. I also have the Anaconda navigator installed. I solved the issue from there.

  1. Open Anaconda Navigator
  2. find “Environments” tab in the top left corner, click on it.
  3. You will see a long list of packages. Click the “Update index…” button at the top of the list.

This solved my issue