jupyter / notebook

Jupyter Interactive Notebook
https://jupyter-notebook.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
11.7k stars 4.94k forks source link

Import on Jupyter notebook failed where command prompt works. #1524

Closed thirumalaipm closed 2 years ago

thirumalaipm commented 8 years ago

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.

takluyver commented 8 years ago

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.

thirumalaipm commented 8 years ago

Thanks takluyver,

I see the difference. The path from the command prompt shows

sys.executable 'C:\Users\user\AppData\Local\Continuum\Anaconda3\python.exe'

Where as the notebook shows 'C:\Users\user\AppData\Local\Continuum\Anaconda3\envs\py35\python.exe'

I had installed py27 and py35 on my system. Can you please let me know how to change to path which is used in command prompt.

Carreau commented 8 years ago

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.

QLQLQL commented 8 years ago

Hi, I'm encountering the same problem here. I've installed successfully tensorflow and anaconda on my Mac OS. Everything goes fine in spyder, while when programming in Jupyter notebook, some modules seem to be not exist and cannot be imported. "Error: no module name matplotlib, scipy....."

~~Then I checked the system and found: In spyder, sys.executable is "/Users/abc/anaconda/bin/python"; In Jupyter, sys.executable is "/Users/abc/anaconda/envs/tensorflow/bin/python" (no matter whether I activate tensorflow in terminal)~~

Both python version are 2.7.11. Now I want to change the python path in Jupyter to which spyder uses. Can anyone please tell me how to do this? Thanks a lot.

See separate issue: #1580

Carreau commented 8 years ago
$ /Users/abc/anaconda/bin/python -m pip install ipykernel
$ /Users/abc/anaconda/bin/python -m  ipykernel install
QLQLQL commented 8 years ago

It works! Thank you so much for your help, really saved my life!

Carreau commented 8 years ago

It works! Thank you so much for your help, really saved my life!

Happy coding !

JoshuaBatty commented 8 years ago

I tried doing what Carreau suggested and although I was able to then import scipy it made it so I could now no longer import matplotlib which I could previously....

takluyver commented 8 years ago

You have one Python environment with scipy installed but not matplotlib, and one with matplotlib installed but not Scipy. Whichever one you pick, you'll have to work out how to install the missing package into it, using conda or pip.

seekFire commented 7 years ago

anaconda python script 2017-07-07 10 22 50

jupyter spyder

I have a similar problem. My problem is that when I launch my anaconda environment and access to Python script. the next line works well: from imgaug import augmenters as iaa but when I execute the same line in jupyter notebook or spyder(both in anaconda), the error occur.

In Python script, the sys.executable:'/home/lqq/anaconda2/envs/tfgpu/bin/python'

My jupyter notebook and spyder have same sys.executable:'/home/lqq/anaconda2/bin/python'

I use the command that @Carreau provide,but only the first one runs well. In second, the error message is: [Errno 13] Permission denied: '/usr/local/share/jupyter'

Carreau commented 7 years ago

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.

seekFire commented 7 years ago

Thank you @Carreau ! I had solved this problem! Eh,,,I just reinstall the imgaug under the anaconda environment,then,it worked OK,,,sounds like a joke,hahaha,,, Thank you for your sincerity!

lucy-itjob commented 7 years ago

Carreau, I have similar problem. I high light our old HDP version 2.4.2.0-258 on the screen below, but we have the new HDP version 2.6, Looks like jupyter pick up the sys.path automatically, where I can find this Jupyter sys.path and what this sys.path name so that I can change this sys.path and get the correct python package

Thannk you for your quick response

jupyter_python_hdp
takluyver commented 7 years ago

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.

pramitchoudhary commented 7 years ago

Great, answers above. I just create symlinks between respective system path and anaconda installation of python, for e.g.

1. get system path !which python2; if the returned path is `/usr/bin/python2`
2. sudo ln -s /home/anaconda2/bin/python2 /usr/bin/python2

this helps me moving from different versions of the python and making the notebooks work consistently.

oscarvilla commented 6 years ago

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.

Codeguyross commented 6 years ago

i ran the following commands up above because it had up votes and it broke my ability to launch a jupyter notebook and killed that environment costing me hours of time to rebuild all the installs. Its unlikely someone will see this before they try it but in case they do

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.

lamiaaAliSaid commented 6 years ago

i have this error please help

ModuleNotFoundError Traceback (most recent call last)

in () 8 import tensorflow as tf 9 from tensorflow.python.framework import ops ---> 10 from cnn_utils import * 11 12 get_ipython().run_line_magic('matplotlib', 'inline') ModuleNotFoundError: No module named 'cnn_utils'
SafinaYousaf commented 5 years ago

The script jupyter-nbconvert.exe is installed in 'c:\users\moosa computers\appdata\local\programs\python\python36\Scripts' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location. how do i resolve this?

jasch-shah commented 5 years ago

Following is outcome of my terminal window: (ComputerVision) MacBook:~ Mac$ python Python 2.7.3 |Continuum Analytics, Inc.| (default, Dec 20 2012, 09:57:39) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin Type "help", "copyright", "credits" or "license" for more information.

import sys sys.executable '/anaconda2/envs/ComputerVision/bin/python'

Following is the output of jupyter notebook:

import sys sys.executable

'/anaconda2/bin/python'

I think issue is with virtualenv ComputerVision ..How to fix this ?

nise commented 5 years ago

So what is the exact solution? I am working on a linux and can't find any documentation from the anaconda and jupyter community.

$ python --version Python 3.6.8 :: Anaconda, Inc.

$ python

import sys sys.executable '/home/abb/anaconda3/envs/something/bin/python'

What are the next steps?

AntoineMack commented 5 years ago

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

Pbilaiya commented 5 years ago

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

MayenUddinSoikot commented 5 years ago

sys.executable

sys. sys.executable '/Library/Frameworks/anaconda3/bin/python'

path sys.path ['/Users/macbookpro/PycharmProjects/PYTHONCODE', '/Library/Frameworks/anaconda3/lib/python37.zip', '/Library/Frameworks/anaconda3/lib/python3.7', '/Library/Frameworks/anaconda3/lib/python3.7/lib-dynload', '', '/Library/Frameworks/anaconda3/lib/python3.7/site-packages', '/Library/Frameworks/anaconda3/lib/python3.7/site-packages/aeosa', '/Library/Frameworks/anaconda3/lib/python3.7/site-packages/IPython/extensions', '/Users/macbookpro/.ipython']

i can't import it in jupyter..i need some help

zabir-nabil commented 5 years ago

https://github.com/udacity/P1_Facial_Keypoints/issues/13#issuecomment-462085023

kazukiotsuka commented 5 years ago

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:

anonymousDog12 commented 5 years ago

Solved it.

Run jupyter kernelspec list in the terminal, or run import sys; sys.executable in an opened jupyter-notebook to see which interpreter it is using, and then we can manually delete these interpreters. Jupyter will then automatically find the other default interpreters (the ones with the right packages correctly installed)

FunkeGoodVibe commented 5 years ago

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.

works perfectly @Carreau

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.

apart from the fact that I had to specify the specific python version.

i.e. 'python -m ipykernel install' changes to:

python3.7 -m ipykernel install.

As I have python3.7 installed on my machine.

Typing python alone reverts back to python2.7.

Thumbs up from me!!

loiloip commented 5 years ago

just upgrade your jupyter notebook :

pip install --upgrade pip pip install jupyter

gmazzitelli commented 5 years ago

I have the same problem by importing ROOT CERN on centos with anaconda and jupyterhub perfectly woking on command prompt but not working in the notebook

ImportErrorTraceback (most recent call last)

in () ----> 1 import ROOT ImportError: No module named ROOT

following the idea of this post I have temporary solved the problem for python3 (default in my coda installation) by the following path added directly before calling the module

import sys
sys.path.insert(0, "/usr/local/root/build")
sys.path.insert(0, "/usr/local/root/build/lib")
sys.path.insert(0, "/usr/local/root/build/etc/notebook")
import ROOT

it' not clear to me how to make it permanent, any help?

elzurdo commented 5 years ago

I got @Carreau 's suggestion to work by doing: python -m ipykernel install

pswpswpsw commented 5 years ago

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

gmazzitelli commented 5 years ago

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']

xenia-Y commented 5 years ago

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

ghost commented 5 years ago

importError Traceback (most recent call last)

in () ----> 1 import graphlab ImportError: No module named 'graphlab' how can I resolve this ?
345123613561 commented 5 years ago

ModuleNotFoundError Traceback (most recent call last)

in ----> 1 import keras ModuleNotFoundError: No module named 'keras' error how to solve it
345123613561 commented 5 years ago

in error how to solve it

sdk-bens commented 5 years ago

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

ai559031 commented 5 years ago
$ /Users/abc/anaconda/bin/python -m pip install ipykernel
$ /Users/abc/anaconda/bin/python -m  ipykernel install

I have a similar issue here...

Screen Shot 2019-09-15 at 11 23 06 pm Screen Shot 2019-09-15 at 11 22 57 pm

The paths are different, but one is in python3 and another is in python... I've tried:

/Users/shinyuwu/anaconda3/bin/python -m pip install ipykernel
/Users/shinyuwu/anaconda3/bin/python -m  ipykernel install

But still not work for me :( Can anyone help me

nikhilrsharma commented 5 years ago

I am also facing similar problem.

sys.executable at command prompt gives: "C:\Users\Public.DESKTOP-6RBQT7L\AppData\Local\Programs\Python\Python37-32\python.exe" and Jupyter Notebook gives "C:\ProgramData\Anaconda3\python.exe".

Not sure what am I supposed to do now. Which command should I use in Jupyter Notebook to install quandl in the folder of Jupyter Notebook?

nikhilrsharma commented 5 years ago

python -m ipykernel install

Doesn't work for me. Says : "EnvironmentLocationNotFound: Not a conda environment: C:\Users\Public.DESKTOP-6RBQT7L.conda\envs\Python3". Don't know what to do now.

nikhilrsharma commented 5 years ago
$ /Users/abc/anaconda/bin/python -m pip install ipykernel
$ /Users/abc/anaconda/bin/python -m  ipykernel install

I have a similar issue here...

Screen Shot 2019-09-15 at 11 23 06 pm Screen Shot 2019-09-15 at 11 22 57 pm

The paths are different, but one is in python3 and another is in python... I've tried:

/Users/shinyuwu/anaconda3/bin/python -m pip install ipykernel
/Users/shinyuwu/anaconda3/bin/python -m  ipykernel install

But still not work for me :( Can anyone help me

Did it work for you? I am facing a similar issue.

ai559031 commented 5 years ago

@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

aiden28 commented 4 years ago

Thank You so much "takluyver"

ZhuoyueLyu commented 4 years ago
$ /Users/abc/anaconda/bin/python -m pip install ipykernel
$ /Users/abc/anaconda/bin/python -m  ipykernel install

Thank you so much!!!!!!!!!!!

asilvam133 commented 4 years ago

You are executing your notebook inside an Anaconda environment. Try the following:

conda install -n curr_env scipy where curr_env is your Anaconda environment name.

Optider commented 4 years ago

Hi @thirumalaipm I had the same issue, this is what worked for me.

  1. Check python version on your terminal/cmd/powershell.
  2. Check version on your Jupyter notebook.

They both need to be the same.

In order to check whether you are having module installed. Type help('modules'), and find the module you are trying to work with. If it is listed there, it means it is installed.

Then there might be some ambiguity in your Python path or version conflict(mentioned at top) as I was having.

pkgdelion commented 4 years ago

ModuleNotFoundError Traceback (most recent call last)

in 24 warnings.filterwarnings("ignore") 25 import syft as sy ---> 26 from syft.frameworks.torch.fl import utils 27 from syft.grid.public_grid import PublicGridNetwork ModuleNotFoundError: No module named 'syft.frameworks.torch.fl'
pkgdelion commented 4 years ago

please I need assistance

kevin-bates commented 4 years ago

As noted multiple times above, given things work in python directly and not from a kernel launched from the notebook server, this is most likely a difference in envs between the two python executables. Locate the kernel.json file (using jupyter kernelspec list) of the kernel you're launching and ensure the python used in the argv stanza is the same as the python used from the command line.

Please check your version of syft in each environment. If older than 0.2.1.a1 then the import should be from syft.frameworks.torch.federated. If its a more recent version, then there might be an issue with the underlying torch installation - I don't know.

Also note that pip install syft reverts both tornado (back to 4.5.3) and notebook (back to 5.7.8) installations - so beware!