notebook: Installed package won't import in notebook
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'
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 87
- Comments: 96 (9 by maintainers)
Complete reinstallation of Anaconda fixed the issue.
Check
sys.executableandsys.pathinside the notebook - I bet your kernel is not running in the environment you intended it to. Runjupyter kernelspec listto see where it finds thepython3kernel.There’ll be a kernelspec somewhere which is pointing it to a different Python. We really need to redesign that system. Run
jupyter kernelspec listin a terminal to see what kernels it knows about. It will point you to directories which containkernel.jsonfiles - one of those will be pointing it to the wrong Python.As it turns out, this does not fix this issue as I first claimed. It is not restricted to the seaborn package either.
Wow, that’s bizarre. Its an extremely odd list. I’ve deleted
~/Library/Jupyter/kernelsand that appears to have straightened things out.Thanks.
It sounds like you’re trying an import in two different installations of Python, or two different environments. Check
sys.executableto see which Python and environment you’re running in, andsys.pathto 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:
If you are using conda environments, you can install packages from conda like this:
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 ]
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.pipthat comes with Anaconda instead of systempip).Steps to replicate:
Create conda env with:
conda create --name tester notebook pymc3 numpy ipython
Activate environment:
source activate tester
Open IPython, and import pymc3 from the command line. Assert that it imports as expected.
Open a Jupyter notebook session:
jupyter notebook
Create a new Python3 notebook and import pymc3. Failure occurs:
Despite all the helpful comments, it took me a while to figure this one out. I’m using pip, not conda, and I got it to work reliably without having to manually change the Python executable path as follows:
source venv/bin/activatepip3 install jupyterChecking
sys.executableshould now point to the venv’s Python kernel. I prefer this method over manually changingkernel.jsonsince that would get tedious if working with multiple different venvs. Pretty much the same idea as @vtslabI 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.
To install on notebook and have any effect the correct way to do it is:
It will install in all conda environments.
If you do:
then it is even better! It will install in all environments (not only conda environments)
Where it says
"python"in that file, give it the path to the Python executable you want it to use.conda install nb_conda_kernelsI tried this. It worked. Original post: https://stackoverflow.com/a/44786736/4110004Install jupyter in that particular environment.
ok,
sys.executablein just python in terminal gives me /Users/hotte/miniconda2/envs/jupyterlab/bin/python (after Isource activatethe 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)Facing the same issue @mgooty
!jupyter kernelspec listwill 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@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/kernelsso that I can follow your strategy?Thanks all for the infomatic post for the issue that I also encountered. And agree with @sandsece i.e.
==========
PROBLEM:
From within myENV created from conda, run jupyter notebook, but in this notebook some module that could be loaded from myENV just through out error like:
ModuleNotFoundError Traceback (most recent call last) <ipython-input-6-ae142f98063f> in <module>() 1 # import libraries ----> 2 import folium
ModuleNotFoundError: No module named ‘folium’
===========
Check:
which jupytersince even sys.path show correct path of myENV, the sys.executable can be different in myENV and in Jupyter notebook. This can simply because the jupyter you run is not installed in myENV and it has to fallback to the base install. It happens that in the base environment the module you want to use is not installed. ----> error!!!===========
Solution:
For the case above, it is straightforward:
conda install jupyterHope this will help.
BR//Kangqiao
Thanks! I reset the path in the kernel.json file as you suggested. Everything works now!
I had the same problem. Changing kernels did not work for me. I installed pandas in my venv but It wont show up in jupyter which I ran through my venv.
I have Ubuntu, with a virtual env in python 3.7
python3.7 -m venv my_envThen I added the venv python site-packages folder to system path and it worked. Added following code to jupyter notebook and it was able to find pandas easilyJust to clarify, I am using a conda env based on this yml:
The env was created and all packages were installed without error, but some packages can be loaded into the notebook, while others were not.
On Ubuntu, I found that using
sudo ~/anaconda3/bin/pipinstead ofpiporpip3worked for me.Additionally, I’ve just started using an alias for pip for convenience with
alias pip="sudo ~/anaconda3/bin/pip"in the terminal.Having the same issue, any ideas / solutions?
I can also confirm that the package is present in the
site-packagesdirectory for the active env:And again, it imports from the IPython console; its only the notebook where it fails.
I faced a similar issue. While the imports were working fine on terminal in the virtual environment, they failed to import in the jupyter notebook. The problem was that I had not installed a local jupyter package for the virtual environment. Therefore, the jupyter notebook on my newly created virtual environment was using the global instance of notebook installation, i.e. from the base environment.
I used the command
to install notebook on my virtual environment, and this fixed all issues for me. I could import all the packages local to the newly created virtual environment.
@1998anwesha Just check where is the Python Interpreter is running? Are they same or different? Both Jupyter and your interpreter must have same interpreter.
import sys print(sys.executable)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:~$pythonPython 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.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
floattonp.floatingis deprecated. In future, it will be treated asnp.float64 == np.dtype(float).type. from ._conv import register_converters as _register_convertersIn [4]:
Wish this helps.
Even installing the old fashioned way with
python setup.py installdoes not result in the notebook seeing the package. I’m at a loss here.Run the following in the jupyter notebook cell:
import sys
sys.path
sys.executable
It may not be pointing to your virtual environment but to the root
The fix is to install the jupyter notebook from inside your virtual environment
$ . your_env/bin/activiate
(your_env)$ python -m pip install jupyter
Now you can import tensorflow or keras
I also had the problem of differences in the
sys.executablefor the console and the jupyter notebook. Both point to different pythons within the same virtual environment. My solution was to install jupyter notebook in the virtual environmentsource activate <your virtual env>conda install jupyter notebookAfter killing and restarting jupyter notebook they are the same 😃I had a similar issue. I was trying to install few python packages in the conda environment. But I was getting
ImportErrorin the jupyter notebook. Though it worked fine in the terminal with the conda env on.I looked at the
sys.executableandsys.pathfrom 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.
Tried installing on an AWS instance. Same problem.
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.executableis what I expect it to be:Hi guys,
After all these years this problem is still annoying. I just wanted to quote what I did to solve a similar problem started by @fonnesbeck a time ago.
After checking the comment of @Vaishak I decided to open my
kernel.jsonfile that is inside the environment that I created for my project: /anaconda/envs/MYPROJECT/share/jupyter/kernels/python3and I changed “python” by “/Users/myname/anaconda/envs/MYPROJECT/bin/python”
This was possible because of the discussion between @ShayekhBinIslam and @Vaishak
Debugged the issue, at least for my case. (Note that as I was debugging this, I found that I did sometimes have to delete the corresponding folder in ~/Library/Jupyter/kernels/ else jupyter would not update, e.g., different versions of the same package even though ipython did).
This doesn’t work:
Same issue here: import works when in ipython command line but not in jupyter (notebook or lab).
This does work:
Haven’t tested doing
conda activate testbut imagine that should work too.This worked for me and seems like a very easy solution.
This is quite a weird bug. Like @iamjli mentioned, if I just reactivate the env by deactivating and activating again, it works fine, even though literally nothing else changed. So that means if you install package and jupyter at the same time, without reactivating env, it will not be able to import, until you reactivate. I just tested this again in a new virtualenv, it proved to be correct.
create a new notebook and try to import numpy. It failed. Try deactivate, and activate again, it works fine. I have a hard time believing such fundamental issue has existed since 2017 and remain unfixed.
On a side note: it has nothing to do with the path inside
kernel.jsonor sys path not appended or anything.I’ve tried summarizing options and common troubles in this Q&A: https://stackoverflow.com/questions/58068818/how-to-use-jupyter-notebooks-in-a-conda-environment
Same issue here, just installed sklearn by conda, by:
conda install scikit-leanrI can import it from command line, but not from jupyter.Well if it will only work if we install a new
jupyter notebookin each new environment, what on earth is the point of having these stupidkernelspecfiles and separate kernels in the base jupyter notebook installation? Also the recommended way of handling multiple environments according to the docs is to use kernels and kernelspecs. But this doesn’t seem to work, instead everyone is doing this workaround of installing a new jupyter notebook on each environment.Why is this issue closed? It is still WIDE open in my opinion!
My problem solved by install Jupyternotebook in the conda environment and use it there. The packages can be imported in the same environment.
Without this step, I think you are using Jupyternotebook from the general setting, for that import packages in a specific conda environment is not working.
For me, I installed Jupyter after creating an environment, but then was trying to run a module installed from the base env. I found by “jupyter kernelspec list” (https://github.com/jupyter/notebook/issues/2563), my kernel.json at C:\Users\username\Anaconda37\share\jupyter\kernels\python3\kernel.json was pointing to the python.exe in my working env. Changed the path and solved it.
This was an exhaustive description of python path setting. And here is my solution.
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, checksys.pathfor differences.@takluyver Thanks for the blog post!
Check
sys.executableanyway, it might be running the kernel somewhere different to where you expect.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).
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.
What if you use a proper terminal instead of trying to run
!pipin the notebook?