关于python:没有为请求命名的模块

No module named for requests

我有两台Ubuntu服务器。在服务器服务器A上输入以下内容可以正常工作:

1
2
3
4
5
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type"help","copyright","credits" or"license" for more information.
>>> import requests
>>> from requests.packages.urllib3.poolmanager import PoolManager

但是,服务器B,

1
2
3
4
5
6
7
8
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type"help","copyright","credits" or"license" for more information.
>>> import requests
>>> from requests.packages.urllib3.poolmanager import PoolManager
Traceback (most recent call last):
  File"<stdin>", line 1, in <module>
ImportError: No module named packages.urllib3.poolmanager

两个版本都相同:

1
2
3
4
Name: requests
Version: 2.7.0
Location: /usr/local/lib/python2.7/dist-packages
Requires:

这是怎么回事?

以下是服务器B的路径:

1
2
3
4
5
6
7
8
9
/usr/local/lib/python2.7/dist-packages/greenlet-0.4.5-py2.7-linux-x86_64.egg
/usr/local/lib/python2.7/dist-packages/gevent-1.0.1-py2.7-linux-x86_64.egg
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages

问题是服务器B的文件位于此处,因此应该找到它:

1
/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3


这是python-requests中的一个问题,因为在这里提到的pypi和ubuntu存储库之间,所有者并没有将其打包。

解决:

  • 直接使用urllib3

    1
    from urllib3.poolmanager import PoolManager
  • 完全删除python-requests的所有副本,而不是使用pip或使用apt-get重新安装,后者包括packages.urllib3内部。