Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...