Pushkar Sharma
Pushkar Sharma

Reputation: 65

ImportError: No module named 'pandas' (inside virtualenv)

I created a virtual environment named quora for python. I installed wheel and then pandas as instructed. I cant get pandas to work for some reason. Can someone help me. I have tried all the other solutions available to similar questions on this website. Still no use.

(quora) (jessie)griffith@localhost:~/environments$ sudo pip install wheel
Requirement already satisfied: wheel in /usr/lib/python2.7/dist-packages
(quora) (jessie)griffith@localhost:~/environments$ sudo pip install pandas
Requirement already satisfied: pandas in /usr/local/lib/python2.7/dist-packages
Requirement already satisfied: pytz>=2011k in /usr/local/lib/python2.7/dist-packages (from pandas)
Requirement already satisfied: numpy>=1.7.0 in /usr/local/lib/python2.7/dist-packages (from pandas)
Requirement already satisfied: python-dateutil in /usr/local/lib/python2.7/dist-packages (from pandas)
Requirement already satisfied: six>=1.5 in /usr/lib/python2.7/dist-packages (from python-dateutil->pandas) 

(quora) (jessie)griffith@localhost:~/environments$ python getdata.py
Traceback (most recent call last):
  File "getdata.py", line 2, in <module>
    import pandas as pd
ImportError: No module named 'pandas'

Upvotes: 6

Views: 12447

Answers (6)

Pedram
Pedram

Reputation: 1843

I tried all the answers before in my archlinux machine but none worked. But this post from Nikolai Janakiev helped me find a good solution.

Solution

Don't forget to first activate the virtual env, mine is named .venv:

$ source .venv/bin/activate
(.venv) $ python3 -m pip install ipykernel

Pick any arbitrary name and replace with NEW_KERNEL, this shows up in your jupyter notebook with the same name:

(.venv) $ python3 -m ipykernel install --name=NEW_KERNEL

And you're done!

Creating Virtual Environments

If you're not already familiar with setting up a virtual environment, here is the official guide.

Let's suppose you want to create a virtual environment under the name .venv. I use the . prefix to hide it by default.

$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ python3 -m pip install pandas

And voila! You have access to the pandas package in .venv environment.

Using Virtual Environments in Jupyter Notebooks

If you're wondering how to activate the virtual environment in a jupyter-notebook, just follow the Solution section above, and then open up the notebook, and click on Kernel then Change Kernel to NEW_KERNEL or the name you picked in the Solution section.

Upvotes: 2

Ronald Mosomi
Ronald Mosomi

Reputation: 1

go to pyvenv.cfg and change

include-system-site-packages = false 

to

include-system-site-packages = true

Upvotes: 0

Bill Ravdin
Bill Ravdin

Reputation: 259

I had the same problem. I fixed it by deleting my virtualenv directory and creating a new environment.

Upvotes: 2

sam
sam

Reputation: 51

I had this problem in a virtualenv with pip3 and pandas, tried all these previous answers, none of which actually work. but you can use easy_install pandas. et voilà.

Upvotes: 3

phd
phd

Reputation: 94808

Don't use sudo in a virtualenv — sudo pip install installs packages into global site-packages, not in virtualenv.

Either install pandas in the virtual environment (pip install after activating venv) or enable access to the global packages (recreate venv with option --system-site-packages or use command toggleglobalsitepackages from virtualenvwrapper).

Upvotes: 2

Nitin Kishore
Nitin Kishore

Reputation: 41

Check "which python" you are running using that command. You may need to export PATH to the python env instead of your default python which might be /usr/lib/bin. It might be installed in your quora env but the python that is being picked up is different and that doesn't have pandas

Upvotes: 0

Related Questions