我很难理解为什么在确保安装httplib2 之后我得到ImportError: No module named httplib2
。见下文:
$ which -a python
/usr/bin/python
/usr/local/bin/python
$ pip -V
pip 1.4.1 from /usr/local/lib/python2.7/site-packages/pip-1.4.1-py2.7.egg (python 2.7
$ pip list
google-api-python-client (1.2)
httplib2 (0.8)
pip (1.4.1)
pudb (2013.5.1)
Pygments (1.6)
setuptools (1.3.2)
wsgiref (0.1.2)
$ pip install httplib2
Requirement already satisfied (use --upgrade to upgrade): httplib2 in /usr/local/lib/python2.7/site-packages
Cleaning up...
$ python
Python 2.7.5 (default, Sep 12 2013, 21:33:34)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import httplib2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named httplib2
我也做过
$ find / | grep httplib2
/usr/local/lib/python2.7/site-packages/httplib2
/usr/local/lib/python2.7/site-packages/httplib2/__init__.py
[... edited for brevity]
管道! &gt;在天堂摇晃拳头&lt;
答案 0 :(得分:40)
如果有多个Python实例(2&amp; 3),请尝试使用不同的pip
,例如:
Python 2:
pip2 install httplib2 --upgrade
Python 3:
pip3 install httplib2 --upgrade
要查看安装的内容和位置,请尝试:
pip list
pip2 list
pip3 list
然后确保您使用正确的Python实例(如other answer中所述)。
答案 1 :(得分:15)
将此添加到.bash_profile
export PATH=/usr/local/bin:$PATH
$ which -a python
/usr/local/bin/python
/usr/bin/python
/usr/local/bin/python
$ python
Python 2.7.6 (default, Dec 27 2013, 14:07:24)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import httplib2
>>>
无法确定为什么pip
安装到/usr/local
而非系统默认设置,但现在它们是相同的,所以它现在正在工作。
答案 2 :(得分:2)
我在Windows 7上遇到了类似的问题。 以下是我解决它的方法:
现在,可以从命令行访问Python。但是,在我的情况下,请致电
py script.py 导致了同样的错误:“ImportError:没有名为httplib2的模块”
然后,我必须将Python和Pip安装路径添加到“Path”环境变量,以便安装httplib2模块,然后执行脚本而不会失败。 我按照here提供的说明进行操作。
然后我就能执行
pip3安装httplib2 --upgrade
最后,我成功地设法执行了包含httplib2 import语句的脚本。
答案 3 :(得分:1)
在Ubuntu上:
使用Ubuntu软件包管理器安装库解决了我的问题:
import java.util.Scanner;
public class MoveFirstWordToLast{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a line of text. No punctuaton please");
String sentence = keyboard.nextLine();
int index = sentence.indexOf (' ');
char word = sentence.charAt(index+1);
//String change = String.valueOf(word).toLowerCase(); //uppercases the new word
String start = String.valueOf(word).toUpperCase(); //uppercases the new word
start = start + sentence.substring (index+2);
start = start +" ";
System.out.println(start);
String end = sentence.substring (0 , index);
end = end.toLowerCase();
System.out.println("I have rephrased that line to read: ");
System.out.println(start + end);
}
}
答案 4 :(得分:0)
如果上述方法均无效,则可以查看特定python正在使用sys.path
检查哪些路径:
$ /usr/bin/python
Python 3.7.6 (default, Feb 26 2020, 20:54:15)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib64/python37.zip', '/usr/lib64/python3.7', '/usr/lib64/python3.7/lib-dynload', '/usr/local/lib/python3.7/site-packages', '/usr/lib64/python3.7/site-packages', '/usr/lib/python3.7/site-packages']
如果该库已经位于以下路径之一下,请检查其权限,因为pip可能仅以root所有者和组权限安装了该库:
$ sudo ls -FsCla /usr/local/lib/python3.7/site-packages
total 8
0 drwxr-x---. 13 root root 274 Jun 5 14:06 ./
0 drwxr-x---. 3 root root 27 Jun 5 14:02 ../
4 drwxr-x---. 3 root root 4096 Jun 5 14:02 requests/
0 drwxr-x---. 2 root root 102 Jun 5 14:02 requests-2.23.0.dist-info/
$ sudo ls -FsCla /usr/local/lib/python3.7
total 0
0 drwxr-x---. 3 root root 27 Jun 5 14:02 ./
0 drwxr-x---. 3 root root 23 Jun 5 14:02 ../
0 drwxr-x---. 13 root root 274 Jun 5 14:06 site-packages/
这是快速修复它的一种方法:
$ sudo find /usr/local/lib/python3.7 -type d -exec chmod 755 {} +
$ sudo find /usr/local/lib/python3.7 -type f -exec chmod 644 {} +
或者您可以使用virtualenv。
答案 5 :(得分:0)
对我来说,在使用pip install
之后,我使用pip list
检查了模块的安装位置。然后,我进入终端中的目录并运行export PYTHONPATH=`pwd`
,将其修复