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

只谈情不闲聊 提交于 2019-12-17 19:42:53

问题


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 this sort of thing.

Computer is a mac mini, osx version 10.9.4

In /Library/Python I have 4 folders: 2.3 2.5 2.6 and 2.7.
In /Applications I have "Python 2.7" and "Python 3.4"
I can open IDLE and type 8+8 and I get 16 just fine.

Here is the error I am getting in terminal:

 host-210-117:~ Mario$ python       
 ImportError: No module named site       

 host-210-117:~ Mario$ pip       
 ImportError: No module named site      

My goal is to run this command in the terminal:

 pip install requests

I believe pip is already installed. I run the file "get-pip.py" in IDLE and this is what it says:

 Requirement already up-to-date: pip in /Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg
 Cleaning up...

This may have something to do with paths? I would appreciate some guidance/hints/tips, thanks!

Oh and just a little more info that might help solve this question. Here is the first few lines of the program I'm running:

 import base64
 import hmac
 import json
 import requests
 import time
 import urllib
 import os

Which gives me this error in IDLE (so I guess it importing those first few packages with no problem?) :

 >>> 

 Traceback (most recent call last):
   File "/Users/Mario/Desktop/pyak/pyak.py", line 4, in <module>
     import requests
 ImportError: No module named requests
 >>> 

***** System path list:

sys.path ['/Users/Mario/Desktop/pyak', '/Users/Mario/Documents', '/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/Library/Python/2.7/site-packages']

*** another update:

 host-210-117:~ Mario$ which python      
 /Library/Frameworks/Python.framework/Versions/2.7/bin/python

*** a little more info (is this supposed to happen?)

 host-210-117:~ Mario$ which pip       
 /usr/local/bin/pip        
 host-210-117:~ Mario$ pip       
 ImportError: No module named site       

*** After changing .bashrc

.bashrc: here's what in the file

export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
alias python=/Library/Python/2.7/python

here is the output of running:

  pip install requests      

host-210-117:~ Mario$ pip install requests
Downloading/unpacking requests
Downloading requests-2.4.1-py2.py3-none-any.whl (458kB): 458kB downloaded
Installing collected packages: requests
Cleaning up...
Exception:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/basecommand.py", line 122, in main status = self.run(options, args)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/commands/install.py", line 283, in run requirement_set.install(install_options, global_options, root=options.root_path)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py", line 1435, in install requirement.install(install_options, global_options, *args, **kwargs)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py", line 671, in install self.move_wheel_files(self.source_dir, root=root) File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py", line 901, in move_wheel_files pycompile=self.pycompile,
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/wheel.py", line 215, in move_wheel_files clobber(source, lib_dir, True)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/wheel.py", line 205, in clobber os.makedirs(destdir)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/requests'

Storing debug log for failure in /Users/Mario/Library/Logs/pip.log


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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



回答5:


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`



回答6:


sudo easy_install pip
sudo pip install requests


来源:https://stackoverflow.com/questions/25968239/mac-python-import-error-no-module-named-site

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