Mac - Python - import error: "No module named site"

I met the same question, and error info is:

ModuleNotFoundError: No module named 'xxx'

and finally solved by

brew install python3

brew link python3

sudo python3 -m pip install xxx
// or `sudo python3 -m pip install -r requirements.txt`

I fixed mine with:

brew reinstall python

It fixed all my broken paths. I think I broke it with a broken brew package that had a wrong python version dependancy or something like that.


From what I can tell you have three versions of Python on your system.

  • The one that comes with OSX /Library/Frameworks/Python.framework/Versions/2.7/
  • Python 2.7 from python.org /Library/Python/2.7/site-packages
  • Python 3.4 from python.org

pip is installed against the Python 2.7 version you downloaded (the one you see in your Applications folder), unfortunately the default Python for your shell is the one that's bundled with OSX, and there is no pip installed there.

IDLE is also bundled with the Python that you downloaded, which is why it keeps telling you that pip is installed, but it doesn't work from the shell.

Since you are probably using the Python downloaded from python.org as your "primary" Python (after all, its the one with IDLE that you are using), you need to set your shell environment to point to this Python as default.

The easiest way to do that is to add a variable in .bashrc that creates an alias python and points it to the right binary. To do that, add this line to /Users/yourusername/.bashrc - files with . are hidden by default, so you'll have to write the entire file name in the command line to open it. Add the following line:

alias python=/Library/Python/2.7/python

Save the file and then close all terminal windows and open it again. Now type pip and it should work correctly, and then you can proceed to installing requests.

For future reference, try to stick with one version of Python. I personally ignore the bundled version and use the one from brew, but you can stick to the Python downloaded from python.org.