I'm having a hard time understanding why I get ImportError: No module named httplib2
after making sure httplib2 is installed. See below:
$ 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
I've also done
$ find / | grep httplib2
/usr/local/lib/python2.7/site-packages/httplib2
/usr/local/lib/python2.7/site-packages/httplib2/__init__.py
[... edited for brevity]
PLUMBING! >shakes fist at heavens<
If there are multiple Python instances (2 & 3), try different pip
, for example:
Python 2:
pip2 install httplib2 --upgrade
Python 3:
pip3 install httplib2 --upgrade
To check what's installed and where, try:
pip list
pip2 list
pip3 list
Then make sure you're using the right Python instance (as suggested in the other answer ).
added this to .bash_profile export PATH=/usr/local/bin:$PATH
then got:
$ 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
>>>
can't say for sure why pip
was installing to /usr/local
instead of system default, but now they're the same, so it's working for now.
I faced similar problems on Windows 7. Here is how I solved it:
Now, Python should be accessible from the command line. However, in my case, calling
py script.py resulted in the the same error: "ImportError: No module named httplib2"
I then had to add the Python and Pip installation paths to the "Path" environment variable in order to install the httplib2 module and then execute the script without failure. I followed the instructions provided here .
Then I was able to execute
pip3 install httplib2 --upgrade
In the end I successfully managed to execute the script containing the httplib2 import statement.
On Ubuntu:
Installing the library using the Ubuntu package manager fixed my issue:
sudo apt-get install python-httplib2
If none of the above work, you can see which paths a specific python is checking with 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']
If the lib is already immediately under one of these paths, check its permissions as pip may have installed it with only root owner and group permissions:
$ 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/
Here's one way to fix it quickly:
$ sudo find /usr/local/lib/python3.7 -type d -exec chmod 755 {} +
$ sudo find /usr/local/lib/python3.7 -type f -exec chmod 644 {} +
Or you can use a virtualenv.
For me, after using pip install
I used pip list
to check where the module was installed. Then, I went to that directory in terminal and ran export PYTHONPATH=`pwd`
, that fixed it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.