Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
thirumalaipm opened this issue Jun 9, 2016 · 63 comments
Closed

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

thirumalaipm opened this issue Jun 9, 2016 · 63 comments

Comments

@thirumalaipm
Copy link

thirumalaipm commented Jun 9, 2016

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
Copy link
Member

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
Copy link
Author

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
Copy link
Member

Carreau commented Jun 16, 2016

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.

@Carreau Carreau modified the milestone: no action Jun 16, 2016
@minrk minrk added this to the no action milestone Jun 17, 2016
@QLQLQL
Copy link

QLQLQL commented Jun 30, 2016

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
Copy link
Member

Carreau commented Jun 30, 2016

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

@QLQLQL
Copy link

QLQLQL commented Jul 1, 2016

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

@Carreau
Copy link
Member

Carreau commented Jul 1, 2016

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

Happy coding !

@JoshuaBatty
Copy link

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
Copy link
Member

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
Copy link

seekFire commented Jul 7, 2017

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
Copy link
Member

Carreau commented Jul 7, 2017

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
Copy link

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
Copy link

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
Copy link
Member

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
Copy link

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
Copy link

oscarvilla commented Apr 8, 2018

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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 ?

@nikhilrsharma
Copy link

$ /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
Copy link

@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
Copy link

aiden28 commented Nov 13, 2019

Thank You so much "takluyver"

@ZhuoyueLyu
Copy link

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

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

@asilvam133
Copy link

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
Copy link

Optider commented Apr 17, 2020

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
Copy link

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
Copy link

please I need assistance

@kevin-bates
Copy link
Member

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!

@pkgdelion
Copy link

pkgdelion commented Aug 4, 2020 via email

@kevin-bates
Copy link
Member

Ping me via email (located on my GH profile) and perhaps we can try an online meeting to get you going - no promises though.

@sssps
Copy link

sssps commented Sep 29, 2020

ModuleNotFoundError Traceback (most recent call last)
in
1 import tensorflow as tf
2 from tensorflow.keras.layers import Conv2D, Dense, GlobalAveragePooling2D
----> 3 from notebooks.utils import (
4 show_images,
5 gaussian_filter,

ModuleNotFoundError: No module named 'notebooks'

@kevin-bates
Copy link
Member

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.

Not sure what package provides the notebooks module, but this all implies that that package is not installed in the "environment" in which your kernel is running.

@zubairmd
Copy link


ImportError Traceback (most recent call last)
in
5 from scipy.constants import mu_0
6 # simpeg and related packages
----> 7 from pymatsolver import pardiso as solver
8 from SimPEG import (mesh,maps,utils,DataMisfit,Regularization,Optimization,Inversion,Invproblem,Directives)
9 from SimPEG.EM import TDEM

ImportError: cannot import name 'pardiso' from 'pymatsolver' (C:\Users\IITR\anaconda3\lib\site-packages\pymatsolver_init_.p

please solve this error

@kevin-bates
Copy link
Member

Please see this comment from above. The code that is running this import statement is running from an environment that is likely different than the python in which notebook server is running (assuming that import works fine from the command line). My guess is that pymatsolver should be updated in your base user env.

@Deimos-M
Copy link

Deimos-M commented Nov 5, 2020

Problem : Import on Jupyter notebook failed where command prompt works.
Reason : This problem usually occurs when your cmd prompt is using different python and Anaconda/jupyter is using different.
Solution : Follow the following steps :-

  1. Run this code in cmd prompt and jupyter notebook and note the output paths. It will probably be different.
    import sys
    sys.executable

  2. Run this cmd in jupyter notebook.
    !jupyter kernelspec list
    --> Go to that directory and open kernel.json file.
    --> Change the 1st parameter of "argv" ,where 'python'(or python directory) is written with the exact output path of the 1st step, which was executed in cmd prompt. Save the changes.

Note : Problem might be resolved by now . Try importing same library in jupyter notebook again by restarting the kernel, if the kernel does not start then perform the 3rd step.

  1. Run these cmds in cmd prompt.
    python -m pip install ipykernel
    python -m ipykernel install

@JusticeBringer
Copy link

If you're looking for a fix for VS Code you may want to look here.

@VikrantMN26
Copy link

VikrantMN26 commented Nov 30, 2020

I can't install geopandas in my work device due to restrictions. It's one of the reasons I started using jupyter notebook which seemed to work fine, for a while even without installing geopandas, but now it throws the "ModuleImportError" for geopandas, all of a sudden. I haven't changed anything (like version etc). What could be the issue?

Any help would be much appreciated.

@Tros-t
Copy link

Tros-t commented Dec 29, 2020

no moduel named Image
It was working just fine then I tried to implement my code in a function and there is an error, I am done, I wasted 5 hours making a simple Program with Python and R

@kevin-bates
Copy link
Member

Hi @Saad-py - could you please provide the context (e.g., the cell code being invoked) and traceback information in which you see no moduel named Image? This sounds like either an env conflict (like those above) or a conflict between built-in and user-defined modules.

@ishijo
Copy link

ishijo commented Mar 21, 2021

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.

@ivanov
Copy link
Member

ivanov commented Feb 3, 2022

Hey there, I'm going through old issues and it seems to me that it makes sense to close this one.

Thanks everyone and happy hacking! :bowtie:

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests