2

I am using a Mac and have both python 2 and 3 installed. However when I try to run a python 3 script and it says module pandas not found, Then I tried the following commands:

pip3 install pandas  AND 
python3 -m pip install pandas

However the error remains the same. I tried to look at installed location of pip3 and python3 and something is not right. Below is the image of which -a python and pip. How do I fix it?

The python and pip installed location Image

1

Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
Ashish soni
  • 89
  • 1
  • 10
  • can you provide the output of `pip3 list | grep pandas`? – Raymond C. Oct 08 '20 at 07:39
  • 1
    Try to locate your `.../dist-packages/pandas/` directory. I suspect it might be installed in one version of python and then when you run, you actually execute the other. Make sure to always explicitly use pip3 and python3, including from your IDEs. – mapto Oct 08 '20 at 07:41
  • make sure to run your python script with python3. One way to ensure this is to make sure to explicitly add the python3 [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) (instead of generic python) in the start of the script – mapto Oct 08 '20 at 07:53
  • pip3 list | grep pandas only shows "pandas 1.1.3". Even after opening the script using python3 xyz.py I still get pandas not found error – Ashish soni Oct 08 '20 at 08:26
  • Did you manage to find `dist-packages/pandas`? This [answer](https://stackoverflow.com/a/52638888/1827854) should help you do it. – mapto Oct 08 '20 at 09:49

3 Answers3

1

If you have both python versions and want to have python3 as default version, you should set the default path to python3 and pip3.

# for zsh
$ echo "alias python=/usr/local/bin/python3.7" >> ~/.zshrc
# or for bash
$ echo "alias python=/usr/local/bin/python3.7" >> ~/.bashrc

# for zsh
$ echo "alias pip=/usr/local/bin/pip3" >> ~/.zshrc 
# or for bash
$ echo "alias pip=/usr/local/bin/pip3" >> ~/.bashrc

That will run your default operations on python3, assuming that the installed version is 3.7.

Secondly, it will be better to use virtualenv in case of multiple python environments

mapto
  • 605
  • 9
  • 23
Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
  • This is dangerous. The reason different linux and bsd distributions (including macosx) still come with the obsolete python2 is that there are system dependencies that haven't been resolved. Overriding the python command, even only in the shell, could lead to unexpected consequences. Besides, it would probably not override python in any IDEs used. – mapto Oct 08 '20 at 07:45
  • Yes, it doesn't help in setting up the IDE runtime environment. There OP needs to choose the python interpreter – Nishu Tayal Oct 08 '20 at 07:50
  • I agree with @mapto Besides I need python2 as my default interpreter. I just need python3 for a small task. My only problem is, whatever package pip3 installs, it installs for python2 and no package is getting installed for 3. I want the set up to be like, pip will install for python 2 and pip 3 to install for python 3 – Ashish soni Oct 08 '20 at 09:44
  • 1
    I suspect your interpretation (pip3 installs for python2) is wrong. We need to understand what actually is happening. That's why I wanted to have hard evidence where your pandas is installed. – mapto Oct 08 '20 at 09:50
1

So it seems pip3 was installed in 2 different directories. /usr/local/bin/pip3 and /usr/bin/pip3 and python3 was installed in /usr/bin/

I used "rm /usr/locak/bin/pip3" to remove pip3 from default location, Now both pip3 and python3 are in /usr/bin/ and now I can install packages using "pip3 install --user pandas" for python3 and "pip install pandas" for python 2.

Thank you @mapto for your idea to check installation directories.

Ashish soni
  • 89
  • 1
  • 10
  • So in your case pip3 is installed both as regular user and as root. Now that you have more clarity of the situation, if suggest you keep only one of the two installations and use it consistently in future. If for any reason (commonly this is library version incompatibles) you need more than one installation of the same python version, the correct way to approach this would be pipenv or virtualenv. – mapto Oct 08 '20 at 21:38
  • great answer but there is a typo so be careful. Correct code would be "rm /usr/local/bin/pip3" – Abduladil Sunnat Dec 23 '22 at 21:20
0

please look into virtualenv (and optionally virtualenvwrapper) . those will isolate your in-built Python and Installed Python versions and modules. so you can avoid the overlaps / conflicts.

  • 1
    Not really an answer to the question, but could resolve the problem. Alternatively to virtualenv, consider also using [pipenv](https://stackoverflow.com/questions/46330327/how-are-pipfile-and-pipfile-lock-used). – mapto Oct 08 '20 at 07:43
  • 1
    Yes Virtual Environment solves the problem. However it would be good if there was a permanent fix. For the time being this will do. Thanks a ton – Ashish soni Oct 08 '20 at 11:16