Mac - Python - import error: “No module named site”

╄→尐↘猪︶ㄣ 提交于 2019-11-28 11:13:18

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.

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.

site.py is a standard module that is run by python by default. It allows tweaking sys.path and running some code before your code starts running. It should live in the standard library and can hardly be somehow absent. However, you can disable automatic importing of the module by passing -S switch to python.

Anyway, you should somehow inspect why the module can not be imported. Try to examine sys.path list.

Sanchay

You are trying to install the package in '/Library/Python/2.7/site-packages/requests' but it requires root permissions to do so. This should do the trick:

$ sudo pip install requests

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`
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!