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

后端 未结 6 1344
我在风中等你
我在风中等你 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:09

    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
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-10 15:12

    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.

    0 讨论(0)
  • 2020-12-10 15:14

    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`
    
    0 讨论(0)
  • 2020-12-10 15:21

    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.

    0 讨论(0)
  • 2020-12-10 15:21
    sudo easy_install pip
    sudo pip install requests
    
    0 讨论(0)
提交回复
热议问题