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

后端 未结 6 1350
我在风中等你
我在风中等你 2020-12-10 14:52

Tonight I am trying to get the package called \"requests\" installed and have begun fumbling around with the terminal and do not have very much intuition when it comes to th

6条回答
  •  臣服心动
    2020-12-10 15:10

    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.

提交回复
热议问题