Unable to import pandas on Jupyter Notebook - python-3.x

I've installed both pandas and jupyter notebook on my virtualenvs(python3).
When I check which jupyter, it's correctly refer to my envs. However, I can't import pandas when I run it in the browser.
When I run the ipython, I can import the pandas module though...
ImportError: No module named 'pandas'
Any help?

Jupyter is built with the Anaconda Python distribution (and the conda environment manager) in mind. Although some work has been done to remediate this issue, using jupyter with virtualenv is discouraged.
In this case it sounds like jupyter is using your default (root) Python environment, because it's not aware of your virtualenv one. See here for details on how to fix that.

If you land here looking for the answer, this is it (taken from the question #Aleksey Bilogur refers):
source activate ENVNAME
pip install ipykernel
python -m ipykernel install --user --name ENVNAME --display-name "Python (whatever you want to call it)"
It is really important that you install ipykernel in your virtualenv and that you launch jupyter notebook also from you ENVNAME.
Once you've done that, check if the path to your ENVNAME appears when executing from your notebook:
import sys
sys.path
If it does not, something went wrong and it won't work.

Related

Module seems to be installed yet Jupyter Notebook won't recognize module

I attempted to exectue the following code:
df = pd.DataFrame()
for file in files:
if file.endswith('.xls'):
df = df.append(pd.read_excel(file), ignore_index=True)
df.head()
but received this error on the 4th command line:.
However, when I check modules installed I receive:
Why does Jupyter not recognize xlrd as being installed? Thanks for any feedback or help.
Problem
You are using pip python's default package manager to check a package in python's default install location.
But using Anaconda's virtual environment (which probably jupyter notebook is used with) to run python script which requires packages to be installed in it's own directory via conda package maanger.
Solution
Run this command in Anaconda command pompt:
conda install -c conda-forge xlrd

conda installed some package but still ModuleNotFoundError when import this package

I've found the solution:anaconda - graphviz - can't import after installation
I want to use graphviz and follow the commend in https://anaconda.org/anaconda/graphviz
run following in terminal
conda install -c anaconda graphviz
However no matter in Jupyter Notebook, python or Pycharm to import graphviz, it always shows
ModuleNotFoundError: No module named 'graphviz'
How to solve this problem? Thank you.
PS:
when run which python in terminal: it return /opt/anaconda3/bin/python, therefore I use anaconda environment by default. And I have only one environment in anaconda that is root.
when I run conda list in terminal, I can find this line :
graphviz 2.40.1 hefbbd9a_2
I found a weird thing:
my pip and conda use the same environment:
run :which pip
get : /opt/anaconda3/bin/pip
run : which conda
get : /opt/anaconda3/bin/conda
However when I run pip list, I cannot find graphviz and many other packages which shows in conda list. For these packages show in conda list but not in pip list, I also cannot import them no matter in Jupyter notebook, python, pycharm etc. Why this happens?
After using "conda install attrs", other package installations are working fine without any http connection or ModuleNotFoundError errors. Please try and let me know.

No module named 'pandas_datareader' in Jupyter (Anaconda) after I run pip3 install pandas_datareader

I am trying to learn pandas and want to load some stocks data. I was following a course which advised me to load pandas.io.data, but this did not work as io.data was depreciated. So I decided to use pandas-datareader instead. But I am struggling to instal it on mac in Anaconda (Jupiter notebook).
First time I run import pandas_datareader as pdweb I got ModuleNotFoundError: No module named 'pandas_datareader'. Not surprising as I never used this before so I run pip3 install pandas_datareader in Terminal which successfully installed itself. However, Jupiter notebook is still giving me the same error. At this point, I tried running in Terminal conda install -c https://conda.anaconda.org/anaconda pandas-datareader but it did not work as -bash: conda: command not found.
Please help. I am looking for a detailed explanation as I am not too techie.
What I run in Jupiter
import numpy as np
from pandas import Series, DataFrame
import pandas as pd
import pandas_datareader as pdweb
import datetime
Result
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-6-da568f513c93> in <module>
2 from pandas import Series, DataFrame
3 import pandas as pd
----> 4 import pandas_datareader as pdweb
5 import datetime
ModuleNotFoundError: No module named 'pandas_datareader'
Just run
conda install -c anaconda pandas-datareader
as per instructions here.
In my experience, if you're using conda, you should never install with pip unless you're sure conda doesn't have it. Try searching anaconda.org to see which -c source to use.
The problem was with conda command not being set up during the installation of Anaconda. I solved this by removing Anaconda with App Cleaner & Uninstaller Pro (free soft) and reinstalling later version.
After reinstallation of Anaconda command below worked like a charm.
conda install -c anaconda pandas-datareader
If you using Anaconda and still have issues installing panads-datareader using the conda command 'conda install -c anaconda pandas-datareader'. or the installation is successful but the import pandas_datareader.data as web still gives an error.
Locate where your 'site-packages % ' directory and cd to the site-packages % dir and then run the installation command 'conda install -c anaconda pandas-datareader '. It will work.
For the install, try using
pip
instead of
pip3

Imported package not available in Jupyter-Python

Importing pysftp into Jupyter Notebook
While importing pysftp into Jupyter Notebook, ModuleNotFoundError is shown.
Checking import of pysftp on device?
I have verified the package installation with
pip list and pip show pysftp
Had imported pysftp package(v0.2.9) and installed it in the below location
C:\users\xxxxxx\appdata\roaming\python\python37\site-packages
Check : Package installed OKAY
Check about package correct path linking from cmd prompt?
I'm using Python 3.7.0 on a WIN machine, verifyed the site package location using
import sys and sys.path
image confirms linking of PATH to correct location and the package is successfully executed when python is run through cmd prompt
Check : Path link and cmd run OKAY
Now could anyone help me solve why the package import in Jupyter Notebook is throwing an error?
Thank you
Edit 1: Check for different environment installed? added based on one of the answer
Only one environment is present in the machine
I got the same. I solved by installing directly within Jupyter using the following command:
import sys
!{sys.executable} -m pip install dice-ml
Are you running the notebook through a virtual environment?
You can try running the same commands as you did on CMD by preceding it with ! as follows:
!pip list
Ideally this should list the same contents as shown in CMD. However the results may be different if you are running Jupyter notebook in a virtual environment. If you are unable to see pysftp, you need to install it within the virtual environment. This can be done from within your notebook as:
!pip install pysftp

Scipy cannot be imported in Jupyter Notebook

I'm trying to use scipy in a jupyter notebook and it says I have it installed, but when I try to import it, it gives me the following error.
Any help would be great. thank you.
TLDR: try this
import sys
!{sys.executable} -m pip install scipy
A bit more info:
Jupyter notebooks are able to work with multiple kernels, which are essentially pointers to the Python (or other language) executable that the notebook uses. In a Python kernel, you can figure out which one is being used by typing
import sys
print(sys.executable)
When you run a bash command in the notebook, like !pip install scipy, that uses the bash environment that was active when you launched the notebook which is not necessarily associated with the Python kernel you are using. That means that it may be installing scipy in a different Python location. You can figure out which Python your shell points to by running !which python. If this doesn't match, then !pip install will not be installing in the right place.
You can fix this by explicitly telling the bash prompt which Python/pip you want to use. For example, this should do the trick:
import sys
!{sys.executable} -m pip install scipy
This runs the pip version associated with your executable, and installs scipy with that. For some more details on what's happening behind the scenes, check out this answer.
The pip that you execute is using a shell that may (and that is probably the case here) have a different python interpreter than the one of the jupyter notebook!
Jake VanderPlas to the rescue https://twitter.com/jakevdp/status/841791667472543745
Do the following in the notebook
import sys
sys.executable
Depending on the output of "sys.executable", adjust your command-line call, still in the notebook
!/usr/bin/python3 -m pip install scipy
But as you have scipy installed, the issue is to understand why your anaconda scipy is not found. Do you have a default virtualenv for all your python use? How do you start the notebook?

Resources