49

I am using the Jupyter notebook with Python 3 selected. On the first line of a cell I am entering:

import pandas as pd

The error I get from the notebook is, ImportError: No module named 'pandas'. How can I install pandas to the jupyter notebook? The computer I launched the Jupyter notebook from definitely has pandas.

I tried doing:

!pip install pandas

And it says it is already installed but for Python 2.7 at the bottom. My script shows it is a Python 3 script at the top though.

When I do echo $PATH in Ubuntu is shows that '/home/user/anaconda2/bin' is on the first entry. I think I may need to change this to be anaconda3?

UPDATE: When I try and launch a Python3 script through jupyter the command line which launched Jupyter gives me the error "ImportError: No module named 'IPython.paths'. Then there is a timeout waiting for 'kernel_info' reply. Additionally, I tried removing anaconda but still experience the same error. I have tried to make so many quick fixes now, that I am not sure what the next step is to get this working.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
William Ross
  • 3,568
  • 7
  • 42
  • 73
  • 2
    you have to run the `pip` version related to your python 3 installation with the full path, not the one in the path. On windows, it is located in `python-installation/scripts/pip.exe` – Jean-François Fabre Nov 14 '16 at 10:36
  • I'm using Linux and yea it looks like typing 'which pip' was using using version 2. I tried to run version 3 though by typing '!/home/user/anaconda3/bin/pip install pandas' and it said all the requirements were satisified... is there some other command I need to tell it to switch to using python 3? – William Ross Nov 14 '16 at 10:43

15 Answers15

48

As your default python version is 2.x , if you don't have any emphasis on the python 3.x you can try from the first by the below scripts.

pip install --upgrade pip
pip install jupyter

then in jupyter notebook:

!pip install pandas

The version of notebook will be 2.x. Otherwise install pip3 by the below Linux commands.

sudo apt-get install python3-setuptools
sudo easy_install3 pip

now you can add pandas to the notebook by !pip3 install pandas.

amin
  • 1,413
  • 14
  • 24
  • Those first 2 commands return that those are satisfied. I realized when i do echo $PATH at my terminal it has '/home/user/anaconda2/bin' on my path... maybe this is the problem? I think i need that to be anaconda3 – William Ross Nov 14 '16 at 11:07
  • After doing those last 2 commands I have the pip3 commands but my jupyter notebook crashes now when trying to start it. One error it says is: 'ImportError: No module named 'IPython.paths'. It also later says 'KernelRestarter: restart failed'. Maybe I need to reinstall jupyter now? – William Ross Nov 14 '16 at 11:24
  • 1
    I'm using python 2.x. For answering your question, after those commands i reinstalled jupyter for python 3.x by `pip3 install jupyter`, and it starts without any problem. In fact with the last installation,the current notebook is based on python 3.x. – amin Nov 14 '16 at 11:29
  • The pip3 install jupyter command ran fine, but I am still getting that ImportError about no module name 'IPython.paths' when I try and open jupyter notebook. – William Ross Nov 14 '16 at 11:39
  • check these [No module named 'IPython.paths'](https://github.com/jupyter/notebook/issues/1535) & [ImportError: No module named paths while running the jupyter notebook](https://github.com/jupyter/notebook/issues/270). – amin Nov 14 '16 at 11:49
  • 1
    I tried upgrading both jupyter and ipython, but no luck with that error. When I do 'pip list' it shows my ipython as 2.4.1, and when I do 'python -m IPython --version' it shows it as 5.1.0. Will dig into that thread a bit more... – William Ross Nov 14 '16 at 20:07
  • Hey, I gave you the bounty because you answer and comments most led to fixing it. The ipython import was solved by removing the *.pth /usr/local/lib/python3.5/dist-packages. Originally I did the same but for python2.7 because the instructions had that which is why those links you listed initially did not solved it. Thanks for the help. – William Ross Nov 21 '16 at 10:27
  • 1
    I'm glad that your problem is solved, and thank you. – amin Nov 21 '16 at 10:28
45

This worked for me

  1. in Jupiter notebook

     import sys
     print(sys.executable) 
    
  2. copy path eg:

      /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
    
  3. install the module on the terminal like this.

     /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install pandas
    

Or it can be installed directly from Jupyter Cell as follows

! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install pandas
Abu Shoeb
  • 4,747
  • 2
  • 40
  • 45
9

If you use anaconda already as a distribution, stop using pip in that context. Use conda instead and you will stop having headaches. The command lines and procedures for setting up a new environment are pretty well documented here.

Basically upgrading python or having specific branches:

conda update python
conda install python=3.5

Or using specific environments:

conda create -n py35 python=3.5 anaconda
Zeugma
  • 31,231
  • 9
  • 69
  • 81
  • I installed anaconda recently because a tutorial I was trying recommended it. My environment is quite messed up now after trying a number of quick fixes... when I try doing 'conda update python' I get an error 'Import Error: No module named ruamel.yaml.comments'. I was wondering if I create a new environment with venv and launch the jupyter notebook from there will it use the venv, or will it use the system settings still? – William Ross Nov 16 '16 at 11:22
  • 2
    In such case, I would suggest to reinstall anaconda from the scratch. It does not take so long. – honza_p Nov 17 '16 at 10:40
  • I've removed anaconda according to some instructions to remove it safely, but I still ge this No module named 'IPython.paths' error when trying to launch a python3 script in jupyter – William Ross Nov 18 '16 at 17:34
  • Just wanted to mention that I need other files on this machine, so reinstalling the OS is not an option. I am looking to fix the problem on just current machine as is without deleting functionality. My OS is the latest version of Ubuntu 16.04 – William Ross Nov 18 '16 at 21:52
  • I suspect you didn't install anaconda by the book and you decided to tune it during installation like changing the default recommended path etc. You look like quite an exception as anaconda is making its leading reputation today by removing all the hassle of installers, packages and so forth. But at this point it's not a coding question anymore so you should switch to super user – Zeugma Nov 19 '16 at 03:27
  • I didn't change any defaults for anaconda, but I had some of the libraries such as numpy already installed before installing anaconda. Could that be a part of the conflict? It looks more like an issue with jupyter than anaconda right now. – William Ross Nov 19 '16 at 11:00
  • If you use `conda create -n` where is the environment file/directory is created? – Islam Azab Feb 02 '21 at 17:21
7

simple step to resolve the problem is

( NOTE: Remember what you have selected python 2 or python 3).

for python 2+

!pip install pandas

or if you have user permission error type

!pip install pandas --user

for python 3+

!pip3 install pandas

or if you have user permission error type

!pip3 install pandas --user
Abdullah Akhtar
  • 529
  • 5
  • 14
  • 1
    Tagging this with **the updated command to run** inside a notebook / `.ipynb` file is `%pip install pandas`. The magic version of the command was added to insure the installation occurs in the proper environment. See [here](https://stackoverflow.com/a/75072786/8508004) for more about this (including links therein) and how the use of the exclamation point doesn't insure this and can lead to issues. – Wayne Jan 17 '23 at 18:55
5

If pip3 is install you could run

!pip3 install pandas
Axel Juraske
  • 186
  • 4
  • When I try that through the jupyter notebook is stays pip3 is not found. When I try to install pip3 locally it says 'Could not find a version that satisfies the requirement pip3 (from versions: ) No matching distribution found for pip3' – William Ross Nov 14 '16 at 10:45
  • As I install jupyter and anaconda I have some similar problems. I remove the anaconda entry in the .bashrc file and restart the shell. That remove the anaconda folder from the PATH variable. – Axel Juraske Nov 14 '16 at 11:21
2

This is what i have done in my system:

I have installed both anaconda for python 2.7 and anaconda for python 3.5. Anaconda helps keep both the environment separate.

In Ubuntu:

The directory structure is like this: anaconda2/bin/ anaconda3/bin/

Whenever i want to use python 2.7 i go to anaconda2/bin/ and create an environment or activate already existing environment and install or import all the necessary packages and same goes for python3.5 (go to anconda3/bin/ create or activate the required environment). This helps me keep things separate.

Since you are using anaconda you should first use "conda install " if that package is not found, then you can use pip install .

In Windows:

If you install both anaconda2 and anaconda3, its quite easy.. the shortcuts for anaconda prompt are in C:\Users\your-username\

there will be two folders anconda2 and anaconda3, you can start conda prompt for python2.7 from anaconda2 and python3.5 from anconda3

So, once you start the anaconda prompt you can just type "jupyter notebook" to open jupyter notebook in browser and import pandas(or any package).

You can check this link:

http://conda.pydata.org/docs/test-drive.html#managing-conda

Yaman Ahlawat
  • 477
  • 6
  • 17
1

I meet the same problem in jupyter notebook, and I run the command below and solve my problem:

!pip install pandas
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
zhao
  • 1,094
  • 9
  • 9
  • Tagging this with **the updated command to run** inside a notebook / `.ipynb` file is `%pip install pandas`. The magic version of the command was added to insure the installation occurs in the proper environment. See [here](https://stackoverflow.com/a/75072786/8508004) for more about this (including links therein) and how the use of the exclamation point doesn't insure this and can lead to issues. – Wayne Jan 17 '23 at 18:56
1

For Windows

The first step is to create a new conda environment. A conda environment is like a virtualenv that allows you to specify a specific version of Python and set of libraries. Run the following commands from a terminal window:

conda create -n name_of_my_env python

This will create a minimal environment with only Python installed in it. To put your self inside this environment run:

source activate name_of_my_env

On Windows the command is:

activate name_of_my_env

The final step required is to install pandas. This can be done with the following command:

conda install pandas

To install a specific pandas version:

conda install pandas=0.20.3

To install other packages, IPython for example:

conda install ipython

To install the full Anaconda distribution:

conda install anaconda

If you need packages that are available to pip but not conda, then install pip, and then use pip to install those packages:

conda install pip
pip install django
Community
  • 1
  • 1
Abdullah Akhtar
  • 529
  • 5
  • 14
  • I have `python 3.7` and `anaconda3` installed in Win10, and `conda 4.7.12`. Had installed jupyter using pip (pip 19.3.1). I can see all packages in the the anaconda directory. I have jupyter notebook - but for just about every package jupyter is complaining - package not found. do I have to do a pip install inside jupyter for every package? – Sam-T Dec 28 '19 at 10:02
1

Simple solution : In a Notebook's cell type and execute the code:

import sys  
!{sys.executable} -m pip install pandas
Aniket Malik
  • 165
  • 1
  • 10
0

Iuse window 7 for work and I had the same problems when I tried to import Pandas. So I tried to install packages under each environment:

Run cmd and type the following code:

activate py27 
conda install pandas 

If the system asks you do you want to install the following new packages, choose Y for [yes]

And install pandas for each different environment if you installed Python27, Python 35 and Python 36, as I did.

Then problem solved if you run jupyter notebook again and you can import pandas successfully.

You can also solve the same problem for packages like numpy, scipy, etc..

0

Maybe its a broken (pip) installation. Following worked for me:

sudo apt --fix-broken install

Followed by:

sudo pip3 install pandas

Hope this helps.

tilakmukul
  • 116
  • 1
  • 9
0

I was getting the error

modulenotfounderror: no module named 'pandas'

in jupyter. I tried the command:

!pip install pandas

and it worked like a charm.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Santosh sanwal
  • 134
  • 1
  • 4
  • 15
  • Tagging this with **the updated command to run** inside a notebook / `.ipynb` file is `%pip install pandas`. The magic version of the command was added to insure the installation occurs in the proper environment. See [here](https://stackoverflow.com/a/75072786/8508004) for more about this (including links therein) and how the use of the exclamation point doesn't insure this and can lead to issues. – Wayne Jan 17 '23 at 18:52
0

My pandas version was 0.20.3 I have updated to 0.25 using conda update pandas. I have checked in Command Line Interface(CLI) its pd.__version__ '0.25.1'.

In Jupyter notebook its showing '0.20.3'.

Please restart jupyter notebook. note is cache your pandas. or you can create a new notebook

0

One silly mistake that you could make is to invoke Jupyter with your local machine python installation and not the anaconda python. I had the same problem and just setting the PATH did not work for me.

FIX: By default, anaconda binaries are in "<Path_to_anaconda>/bin". For example, in my case, they are in "/home/klakhotia/anaconda3/bin". This directory will also contain the binary to anaconda jupyter. Launch the jupyter from here or create an alias in your .bashrc that points to this file.

User2332
  • 66
  • 3
0

What worked for me was:

pip3 install pandas jupyter