jupyter / notebook

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

Installed package won't import in notebook #2359

Closed fonnesbeck closed 7 years ago

fonnesbeck commented 7 years ago

I'm using the notebook from a conda env, and have the environment set up with all of my required packages installed. However, when I try to import one of these (seaborn) in the notebook, I get an import error suggesting it is not installed. This works fine from the command line and I can confirm using the conda command within the notebook that the package is installed:

Running notebook 4.4.1 from conda.

Interestingly, when I try to import notebook from inside of a notebook, I get the same error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-2-09fdb2483669> in <module>()
----> 1 import notebook
      2 
      3 notebook.__version__

ImportError: No module named 'notebook'
fonnesbeck commented 7 years ago

To followup, I have even created a fresh conda env, but I get the exact same error.

Here also is my conda info:

Current conda install:

               platform : osx-64
          conda version : 4.2.13
       conda is private : False
      conda-env version : 4.2.13
    conda-build version : 2.0.2
         python version : 3.5.2.final.0
       requests version : 2.13.0
       root environment : /Users/fonnescj/anaconda3  (writable)
    default environment : /Users/fonnescj/anaconda3/envs/dev
       envs directories : /Users/fonnescj/anaconda3/envs
          package cache : /Users/fonnescj/anaconda3/pkgs
           channel URLs : https://conda.anaconda.org/conda-forge/osx-64
                          https://conda.anaconda.org/conda-forge/noarch
                          https://repo.continuum.io/pkgs/free/osx-64
                          https://repo.continuum.io/pkgs/free/noarch
                          https://repo.continuum.io/pkgs/pro/osx-64
                          https://repo.continuum.io/pkgs/pro/noarch
            config file : /Users/fonnescj/.condarc
           offline mode : False
fonnesbeck commented 7 years ago

I can further confirm that installing Seaborn via pip does not fix this issue.

fonnesbeck commented 7 years ago

Complete reinstallation of Anaconda fixed the issue.

fonnesbeck commented 7 years ago

As it turns out, this does not fix this issue as I first claimed. It is not restricted to the seaborn package either.

fonnesbeck commented 7 years ago

This occurs with pip-installed packages as well as conda:

ccordoba12 commented 7 years ago

I think you need to restart the kernel for this to work. Besides, you have to be sure to be using the right pip (e.g. pip that comes with Anaconda instead of system pip).

fonnesbeck commented 7 years ago

I've restarted the kernel multiple times, but this continues to occur. I have even tried deactivating and reactivating the conda env. I can also confirm that it is the pip installed into the current env, rather than the one in the root env. I can import this library from the ipython console without issue.

I can also confirm that sys.executable is what I expect it to be:

'/Users/fonnescj/anaconda3/bin/python'
fonnesbeck commented 7 years ago

Just to clarify, I am using a conda env based on this yml:

name: dev

channels:
 - conda-forge

dependencies:
- python=3.6
- arrow
- basemap
- blaze
- bokeh
- cython
- dask
- engarde
- ipython
- jupyter
- notebook
- ipyparallel
- ipywidgets
- joblib
- jupyter_client
- jupyter_core
- jupyter_contrib_nbextensions
- jupyter_nbextensions_configurator
- jupyterlab
- line_profiler
- mkl-service
- numexpr
- numba
- numpy
- openpyxl
- pandas
- patsy
- pillow
- pip
- pylint
- scikit-learn
- scipy
- seaborn
- snakeviz
- sympy
- tensorflow
- theano
- xlrd
- pip:
  - GPy
  - PyCap
  - pystan
  - rpy2
  - git+https://github.com/GPflow/GPflow.git#egg=GPflow
  - git+https://github.com/pymc-devs/pymc3.git#egg=pymc3

The env was created and all packages were installed without error, but some packages can be loaded into the notebook, while others were not.

fonnesbeck commented 7 years ago

Even installing the old fashioned way with python setup.py install does not result in the notebook seeing the package. I'm at a loss here.

fonnesbeck commented 7 years ago

Tried installing on an AWS instance. Same problem.

ccordoba12 commented 7 years ago

What if you use a proper terminal instead of trying to run !pip in the notebook?

fonnesbeck commented 7 years ago

I was only using pip in the notebook to list the installed package. It was installed first with conda when I created the env and then using pip from a conventional terminal.

fonnesbeck commented 7 years ago

I can also confirm that the package is present in the site-packages directory for the active env:

drwxr-xr-x   24 fonnescj  staff      816 Apr  2 22:17 pymc3
drwxr-xr-x    7 fonnescj  staff      238 Apr  2 22:17 pymc3-3.0-py3.5.egg-info
-rw-r--r--    1 fonnescj  staff  1124023 Apr  2 14:30 pymc3-3.1rc3-py3.5.egg

And again, it imports from the IPython console; its only the notebook where it fails.

fonnesbeck commented 7 years ago

Steps to replicate:

  1. Create conda env with:

    conda create --name tester notebook pymc3 numpy ipython

  2. Activate environment:

    source activate tester

  3. Open IPython, and import pymc3 from the command line. Assert that it imports as expected.

  4. Open a Jupyter notebook session:

    jupyter notebook

  5. Create a new Python3 notebook and import pymc3. Failure occurs:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-37bf2a3357ff> in <module>()
----> 1 import pymc3

ModuleNotFoundError: No module named 'pymc3'
fonnesbeck commented 7 years ago

I am able to get pymc3 to import if I install it in the root environment, but not in any environments that I create.

takluyver commented 7 years ago

Check sys.executable and sys.path inside the notebook - I bet your kernel is not running in the environment you intended it to. Run jupyter kernelspec list to see where it finds the python3 kernel.

fonnesbeck commented 7 years ago

Wow, that's bizarre. Its an extremely odd list. I've deleted ~/Library/Jupyter/kernels and that appears to have straightened things out.

Thanks.

mgooty commented 7 years ago

@fonnesbeck Even I am facing the similar issue. I have installed keras but I am not able to import it in the notebook. I am using Mac. Where can I find ~/Library/Jupyter/kernels so that I can follow your strategy?

ClimateYukon commented 7 years ago

Facing the same issue @mgooty !jupyter kernelspec list will display the path but that didn't solve the issue for me. Maybe more of a conda question, that fixes the issue for me : http://stuartmumford.uk/blog/jupyter-notebook-and-conda.html

jlintusaari commented 7 years ago

Also facing the same issue

horsto commented 7 years ago

Having the same issue, any ideas / solutions?

takluyver commented 7 years ago

It sounds like you're trying an import in two different installations of Python, or two different environments. Check sys.executable to see which Python and environment you're running in, and sys.path to see where it looks to import modules.

Python packages must be installed separately for each copy of Python you use, and if you are using virtualenvs or conda envs, packages must be installed into each environment where you need them. Either the package is not installed in one, or a different version of the package is installed.

To install packages to a particular Python installation or environment using pip, you can run it like this:

path/to/python -m pip install ...
# Replace path/to/python with the path of the Python executable (sys.executable)

If you are using conda environments, you can install packages from conda like this:

source activate myenv  # On Windows, just 'activate myenv' (no 'source')
conda install ...

I have written this up as a blog post as well: http://takluyver.github.io/posts/i-cant-import-it.html

[ This is a saved reply because I answer similar questions often - sorry if it doesn't exactly fit your case ]

horsto commented 7 years ago

edit: just noticed that this isn't the jupyterlab thread. sorry for the spam. But maybe the problem is transferable. It works for my "normal" Jupyter notebooks

Thanks for the tips! However: I installed jupyterlab in its own conda env and installed everything in there via conda install ... so it should be there, correct? Still - I cannot import it (restarting kernel, etc. does not work, I am using a python 2.7 kernel, it's running on a Mac).

takluyver commented 7 years ago

Check sys.executable anyway, it might be running the kernel somewhere different to where you expect.

horsto commented 7 years ago

ok, sys.executable in just python in terminal gives me /Users/hotte/miniconda2/envs/jupyterlab/bin/python (after I source activate the conda env) Within the notebook I do the same and it gives me /Users/hotte/miniconda2/bin/python . Which is pretty puzzling? Any ideas why the notebook itself does not point to my environment that I started it from? (notebook and everything WAS installed from within the environment)

takluyver commented 7 years ago

There'll be a kernelspec somewhere which is pointing it to a different Python. We really need to redesign that system. Run jupyter kernelspec list in a terminal to see what kernels it knows about. It will point you to directories which contain kernel.json files - one of those will be pointing it to the wrong Python.

horsto commented 7 years ago

Thanks! I reset the path in the kernel.json file as you suggested. Everything works now!

Vaishak commented 7 years ago

How to reset the path in the kernel.JSON file?

{
 "argv": [
  "python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}

the path to where my sys.executable points to at system: /home/whyshock/anaconda3/bin/python the path to where my sys.executable points to in the virtualenv named plotly: /home/whyshock/vs-workspace/plotly/bin/python

When I run on terminal jupyter kernelspec list python3 /home/whyshock/anaconda3/share/jupyter/kernels/python3

I'm trying to run the jupyter notebook in the virtual environment when I try to run a program I get a modulenotfoundError but if I try to pip install the module, it says requirement already satisfied!

What am I doing wrong and how can I fix this?

takluyver commented 7 years ago

Where it says "python" in that file, give it the path to the Python executable you want it to use.

bitMaestro commented 7 years ago

@takluyver Thanks for the blog post!

mmagnuski commented 6 years ago

@takluyver I'm having the same problem but sys.executable is the same in notebook and console. Below you can see that I can't import a package (installed with python setup.py develop) in the notebook but am able to do so from the console. image (I've been restarting notebook kernel many times)

takluyver commented 6 years ago

Do print(mypy) in the console to see where it's imported from. Is it in the working directory? In that case, it will only be picked up when running in that directory. Otherwise, check sys.path for differences.

mmagnuski commented 6 years ago

The package is not in the working directory. I can see the differences in sys.path but I still don't understand why the same python environment started from the same console has the path to this package or not depending on whether it is run via jupyter notebook. :(

bitMaestro commented 6 years ago

Having this issue bites...

However this post can help, I'm on a Mac and viola it worked. In a nut shell run sys.path in your terminal to locate all your python installs including those in Anaconda. Then locate the package you installed and see what directory it was installed in. Then go into your Jupyter notebook and run sys.path to find where your notebook is installed. If your having issues with the package you should clearly see they are not in the same directories.

Follow the steps in http://takluyver.github.io/posts/i-cant-import-it.html to remedy the issue.

Its frustrating but you'll figure it out.

[image: photo] Tarique Hasheem 510-299-2680 http://www.linkedin.com/in/tariqueh Create your own email signature https://wisestamp.com/email-install?utm_source=promotion&utm_medium=signature&utm_campaign=create_your_own

On Sun, Nov 26, 2017 at 10:37 AM, Mikolaj Magnuski <notifications@github.com

wrote:

The package is not in the working directory. I can see the differences in sys.path but I still don't understand why the same python environment started from the same console has the path to this package or not depending on whether it is run via jupyter notebook. :(

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jupyter/notebook/issues/2359#issuecomment-347028825, or mute the thread https://github.com/notifications/unsubscribe-auth/AYguhTTQI_hankxUuHQk83iqYYsdU0sqks5s6a_QgaJpZM4Mw2Yg .

bitMaestro commented 6 years ago

Regarding your last email....

You have to be in your conda env (source activate name_of_your_conda_env). Then you must install the package with conda install name_of_package.

Otherwise using pip or pip3 will install in a directory you may not want. If the package is not available via conda install you can go into your py directory where the conda env is and us pip but make sure to provide the entire path to the directory for the install to be successful.

[image: photo] Tarique Hasheem 510-299-2680 http://www.linkedin.com/in/tariqueh Create your own email signature https://wisestamp.com/email-install?utm_source=promotion&utm_medium=signature&utm_campaign=create_your_own

On Sun, Nov 26, 2017 at 1:23 PM, Tarique Hasheem tariqueye@gmail.com wrote:

Having this issue bites...

However this post can help, I'm on a Mac and viola it worked. In a nut shell run sys.path in your terminal to locate all your python installs including those in Anaconda. Then locate the package you installed and see what directory it was installed in. Then go into your Jupyter notebook and run sys.path to find where your notebook is installed. If your having issues with the package you should clearly see they are not in the same directories.

Follow the steps in http://takluyver.github.io/posts/i-cant-import-it.html to remedy the issue.

Its frustrating but you'll figure it out.

[image: photo] Tarique Hasheem 510-299-2680 http://www.linkedin.com/in/tariqueh Create your own email signature https://wisestamp.com/email-install?utm_source=promotion&utm_medium=signature&utm_campaign=create_your_own

On Sun, Nov 26, 2017 at 10:37 AM, Mikolaj Magnuski < notifications@github.com> wrote:

The package is not in the working directory. I can see the differences in sys.path but I still don't understand why the same python environment started from the same console has the path to this package or not depending on whether it is run via jupyter notebook. :(

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jupyter/notebook/issues/2359#issuecomment-347028825, or mute the thread https://github.com/notifications/unsubscribe-auth/AYguhTTQI_hankxUuHQk83iqYYsdU0sqks5s6a_QgaJpZM4Mw2Yg .

mmagnuski commented 6 years ago

@tariqueye I have to install some packages from github and still be able to edit so I use python setup.py develop. I can add a line of code to append a relevant path to the sys.path, I just found it surprising that I can import from the console and not from the notebook although the same python env is used in both.

gzewdie commented 6 years ago

@all, I did have the same issue when try to import seaborn. I fixed it by installing seaborn as sudo on ubuntu! sudo pip install seaborn

Then I could import it on jupyter notebook. Hope this helps!

mmagnuski commented 6 years ago

@all For some reason it seems to work on that problematic machine now. 😄 I can't identify what helped.

cateoliveira commented 6 years ago

To install on notebook and have any effect the correct way to do it is:

import sys
!conda install --yes --prefix {sys.prefix} seaborn

It will install in all conda environments.

If you do:

import sys
!{sys.executable} -m pip install seaborn

then it is even better! It will install in all environments (not only conda environments)

Maier123 commented 6 years ago

@ALL I'm having the same issue. I am running python 3.6 on a windows 8 computer and write my code with Notepad++, so i couldn't refer to the conda or jupyter answers. my sys.executble gives me 'C:\Users\MyName\AppData\Local\Pprograms\Python\Python36-32\python.exe' sys.path gives me 'C:\Users\....\Pyhton36-32\lib\site-packages\pyinsane2-2.0.2-py3.6-win32.egg' where pyinsane2 is the module I want to use, which is installed and named as "requirement already satisfied " when running >>>import pyinsane2. (I couldn't tell why it is not the pyinsane2 directory in \site-packages instead of the pyinsane2-2.0.2-py3.6-win32.egg ) How do I get python.exe and my package into the same directory?? Thanks

smeeklai commented 6 years ago

@Maier123 Are you running a notebook on conda virtual env?

Maier123 commented 6 years ago

I'm not sure what that means but I only installed Pyhton 3.6 and Notepad++ on my computer to code. Also, My pyhton is added to the path. I run my scripts with C:\Users\MyName>cd path to file + file.py

smeeklai commented 6 years ago

@Maier123 How about trying to follow these steps first, then creating conda environment. After that, activate the environment, pip install ... whatever you need, and then run jupyter notebook

lyupin commented 6 years ago

conda install nb_conda_kernels I tried this. It worked. Original post: https://stackoverflow.com/a/44786736/4110004

Zhuifeng414 commented 6 years ago

@Vaishak Have you solved the problem? I met the same problem now, and i don't known how to solve it.

smilerz commented 6 years ago

I was having a similar issue, I install tflearn under a conda environment but was getting 'module not found' when attempting to import it even though I had run jupyter from the correct conda environment.

However, I had not installed jupyter in that environment, so it was running from the default installation. Installing jupyter into the target environment corrected the problem.

harmanpreet93 commented 6 years ago

I had a similar issue. I was trying to install few python packages in the conda environment. But I was getting ImportError in the jupyter notebook. Though it worked fine in the terminal with the conda env on.

I looked at the sys.executable and sys.path from inside the jupyter notebook. I found that the kernel wasn't running the intended version of python I wanted. The notebook was taking the python host version since I hadn't installed jupyter in my environment.

So to solve it I installed jupyter notebook from inside of my conda environment. That solved my problem.

mehemken commented 6 years ago

For what it's worth

tl;dr I couln't get import pymc3 as pm to work on Jupyter Lab so I had to resort to notebook. In the grand scheme of things, I think I'll be ok.

The missing piece

The last line of the traceback from import pymc3 as pm is

RuntimeError: To use MKL 2018 with Theano you MUST set "MKL_THREADING_LAYER=GNU" in your environement.

Thus

export MKL_THREADING_LAYER=GNU

seemed to fix python, notebook and ipython, but not lab.

Results

Python

This works. Sure there's a warning, but the import actually imports pymc3.

$ python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymc3 as pm
/home/emilio/anaconda3/envs/bmh/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters

Jupyter notebook

This gives the same warning, but it works.

$ jupyter notebook
# In a cell
In [1] import pymc3 as pm
/home/emilio/anaconda3/envs/bmh/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters

ipython

This gives the same warning as above, but it works.

$ ipython
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.3.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import pymc3 as pm

Jupyter lab

This fails

$ jupyer lab
# In a cell
In [1] import pymc3 as pm
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-2e5c536b7c65> in <module>()
----> 1 import pymc3 as pm

ModuleNotFoundError: No module named 'pymc3'

My system

Conclusion

I've spent enough time on this so I'm just going to use the notebook. I'd love to use Jupyter Lab, but there's a limit to the time I can allocate to troubleshooting environments.

liwei2000 commented 6 years ago

had the same problem (installed python module works in python but not in ipython/jupyter). I found a simple solution to this problem: just look up manually the location of the module and add to the sys.path.

For example, I installed the tensorflow in a conda environment named 'tensorflow3'. The location of tensorflow module can be found as following:

wei@wei-Precision-T7600:~$ source activate tensorflow3 (tensorflow3) wei@wei-Precision-T7600:~$ python Python 3.6.4 |Anaconda, Inc.| (default, Mar 13 2018, 01:15:57) [GCC 7.2.0] on linux Type "help", "copyright", "credits" or "license" for more information.

import tensorflow tensorflow.__file__ '/home/wei/anaconda3/envs/tensorflow3/lib/python3.6/site-packages/tensorflow/init.py'

Now I just add the path: '/home/wei/anaconda3/envs/tensorflow3/lib/python3.6/site-packages' to sys.path in ipython / jupyter, and then the tensorflow module can be imported in ipython or jupyter notebook !

In [1]: sys.path.append('/home/wei/anaconda3/envs/tensorflow3/lib/python3.6/site-packages')

In [3]: import tensorflow as tf /home/wei/anaconda3/lib/python3.6/site-packages/h5py/init.py:36: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type. from ._conv import register_converters as _register_converters

In [4]:

Wish this helps.

soumyapatro commented 6 years ago

I faced the same issue too. What was happening was that my ipython notebook was using the global instance of notebook installation since my newly created conda environment did not have jupyter notebook installed in it. And it was also searching for all the packages referenced in the notebook globally.

The fix was : conda install jupyter-notebook in the newly created environment.

After doing this, when I launched ipynb, it started using the packages in my environment and not the global ones. I also used conda install command for installing all the packages.

To test whether it is a global/environment issue export your ipynb as .python file and try running it from bash. If it works in the environment's bash, you know that your notebook is trying to find packages in the global env.