jupyter / help

:sparkles: Need some help or have some questions? Please visit our Discourse page.
https://discourse.jupyter.org
291 stars 97 forks source link

Installed modules cannot be imported (ModuleNotFound error) #444

Closed gcgruen closed 5 years ago

gcgruen commented 5 years ago

It seems I am not the first one with this issue, however, the other solutions I found around the web (stackoverflow, github issues list, medium blog posts) don't solve my issue.

I have created a virtual environment in which Python 3.7 is successfully running. I also installed some modules within the virualenv (jupyter, pandas, matplotlib, requests, BeautifulSoup etc).

If I run a .py file from the command line, it works perfectly fine, all modules are loaded and used.

However, if I start a jupyter notebook, it already fails upon importing basically any of the installed modules, like for example import pandas as pd(not exclusively for pandas though, but basically for any module.)

It fails with the error message ModuleNotFoundError: No module named 'pandas'

I went on searching for an answer. The most common advice: pandas is not installed. However, it is. At first it wasn't in the right place. I did !which jupyter, which !jupyter-notebookand !which python (separately from the command line as well as from within the jupyter notebook). All three returned with the same path to my virtual environment: /Users/lc_admin.grueng/.virtualenvs/dwdata/bin/jupyter

When I do !which pandas it doesn't return a thing, hinting at pandas not being installed. However, when installing it (no matter via what method), it returned with (dwdata) bmac1025559:bin lc_admin.grueng$ pip install pandas Requirement already satisfied: pandas in /Users/lc_admin.grueng/.virtualenvs/dwdata/lib/python3.7/site-packages (0.23.4)

Looking at the path, I realized it is different from the path for jupyter. Thus, I installed it in the same directory using pip install pandas --force-reinstall --target /Users/lc_admin.grueng/.virtualenvs/dwdata/bin.

If I do !which pandas again, it still doesn't return any path. However, if I cd into said path and ls it definitely is there, together with all the other modules.

screen shot 2018-10-09 at 13 54 00

I guess this is the first part of my problem which I don't know how to solve.

Then I thought: If the module is there, but Jupyter doesn't find it, maybe I need to tell Jupyter where to look. So then in the command line I added the path to the JUPYTER_PATH for datafile directory locations to my .bash_profile using echo "export JUPYTER_PATH=/Users/lc_admin.grueng/.virtualenvs/dwdata/bin:$JUPYTER_PATH" >> ~/.bash_profile, I reloaded the bash profile with source ~/.bash_profile and cat it to see whether the path was successfully added (which it is).

But upon restarting jupyter, still I get the ModuleNotFoundError: No module named 'pandas' error and I am out of ideas how else I could fix it.

Any hints much appreciated. Operating system is Mac OSX High Sierra version 10.13.6, Python version within my virtual environment is Python 3.7.0 (outside Python 3.6.5 if it matters), I installed both with pip and sudo pip3 and I'm also through the idea with upgrades and setuptools for the modules. And I'm not a user of (ana)conda, so if there is a way to solve this without it that would be perfect.

minrk commented 5 years ago

This is typically the result of the kernel not being in the same env where packages are being installed.

The quick way to check is:

  1. which Python is pip using (head -n 1 $(which pip) in a terminal where you are installing packages)
  2. which Python is the notebook using (import sys; print(sys.executable))

If you are installing packages that you can't import, these two will likely not be the same. I suspect that one or the other is not the one you mean to be using.

You might want to check:

jupyter kernelspec list

it's possible that you still have a python3 kernel registered pointing to an older location. If that's the case, you can remove it:

jupyter kernelspec remove python3

and make sure that python3 in your env points to the env itself:

ipython kernel install --sys-prefix
gcgruen commented 5 years ago

Hi @minrk, thank you very much for taking the time to look into this!

So, when I run your first command head -n 1 $(which pip)it returns #!/Users/lc_admin.grueng/.virtualenvs/dwdata/bin/python3

When I check which Python the notebook is using with import sys; print(sys.executable) it returns /Users/lc_admin.grueng/.pyenv/versions/3.6.5/bin/python

So you are right that the second one is not the one I am using, but the first one is.

The jupyter kernelspec list returns python3 /Users/lc_admin.grueng/Library/Jupyter/kernels/python3, so it is just one location and I assume the right one? Thus, I did not remove it but only ran your next line about python3 the env pointing to itself, which does not seem to be the case, because if I run ipython kernel install --sys-prefix this happens: [InstallIPythonKernelSpecApp] WARNING | Installing to /Users/lc_admin.grueng/.virtualenvs/dwdata/share/jupyter/kernels, which is not in ['/Users/lc_admin.grueng/.virtualenvs/dwdata/bin/kernels', '/Users/lc_admin.grueng/.virtualenvs/dwdata/bin/Users/lc_admin.grueng/.virtualenvs/dwdata/bin/kernels', 'kernels', '/Users/lc_admin.grueng/Library/Jupyter/kernels', '/Users/lc_admin.grueng/.virtualenvs/dwdata/bin/../share/jupyter/kernels', '/usr/local/share/jupyter/kernels', '/usr/share/jupyter/kernels', '/Users/lc_admin.grueng/.ipython/kernels']. The kernelspec may not be found. Installed kernelspec python3 in /Users/lc_admin.grueng/.virtualenvs/dwdata/share/jupyter/kernels/python3

I'm not sure what to make of it, but maybe you do? Do you have any hints on how to fix it?

minrk commented 5 years ago

/Users/lc_admin.grueng/Library/Jupyter/kernels/python3, so it is just one location and I assume the right one?

This is probably the issue. This file probably has a reference to the other env. Remove this one and I think everything might be okay.

minrk commented 5 years ago

You probably also want to unset JUPYTER_PATH

gcgruen commented 5 years ago

Hi minrk, thanks very much!

This file probably has a reference to the other env. Remove this one and I think everything might be okay. This first hint already worked, it does import pandas now! \o/

Do I need to unset JUPYTER_PATH in addition and if so how do I do that? Remove any reference in my .bash-profile to the "wrong" virtual env?

Right now my bash-profile is quite messy anyway (as a result of me trying desperately/multiple time to fix this problem):

screen shot 2018-11-13 at 09 37 53
minrk commented 5 years ago

Yeah, try removing all setting of JUPYTER_PATH in your bash_profile. It's rather unlikely that you will want to set it (and it shouldn't point to /bin dirs, it would normally point to dirs like .../share/jupyter).

In an interactive shell you can clear it for the current session with:

unset JUPYTER_PATH
gcgruen commented 5 years ago

Yay, everything seems to be working now. Thank you very much for all your help and your patience!