请求包python未导入

时间:2013-07-05 08:03:24

标签: python python-requests

我已经在我的mac(10.8)上安装了请求包,其他任何包pip

我可以在/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages下看到它。

然而,当我在python脚本中import requests时,我收到一个终端错误: ImportError: No module named requests好像没有安装。

易于安装说它也已安装:

$ easy_install requests
Searching for requests
Best match: requests 1.2.3
Adding requests 1.2.3 to easy-install.pth file

Using /Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages
Processing dependencies for requests
Finished processing dependencies for requests

我唯一能找到的错误就是使用pip进行升级时:

$ pip install requests --upgrade
Downloading/unpacking requests
  Real name of requirement requests is requests
  Downloading requests-1.2.3.tar.gz (348Kb): 348Kb downloaded
  Running setup.py egg_info for package requests

Installing collected packages: requests
  Found existing installation: requests 1.2.3
    Uninstalling requests:
      Successfully uninstalled requests
  Running setup.py install for requests

      File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/requests/packages/urllib3/contrib/ntlmpool.py", line 38
        """
         ^
    SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 130-132: truncated \uXXXX escape

Successfully installed requests
Cleaning up...

为什么不能导入一些想法?感谢

1 个答案:

答案 0 :(得分:2)

这似乎是urllib3中的错误。查看源代码,从引发错误的文件的第33行开始:

def __init__(self, user, pw, authurl, *args, **kwargs):
    """
    authurl is a random URL on the server that is protected by NTLM.
    user is the Windows user, probably in the DOMAIN\username format.
    pw is the password for the user.
    """

字符串中间的\u是非法的。我不仅仅从import requests甚至import requests.packages.urllib3收到此错误,但如果我import requests.packages.urllib3.contrib.ntlmpool,我也会收到此错误。

我不知道为什么它会为你自动导入ntlmpool,但这并不重要;这肯定是一个错误。


该错误已于2013-05-22在change 1f7f39cburllib中修复,并于2013-06-08在change 2ed976ea合并为requests,作为{的一部分{ {3}}。但它仍然出现在1.2.3版本中,这是截至2013-07-05的最新版本issue 1412。您可以在on PyPI中找到更多相关信息,该信息已被关闭,注释“修复程序应该很快就会出来。”

所以,你有三个选择:

  • 等待1.2.4。
  • pip install git+https://github.com/kennethreitz/requests安装github上的top-of-tree代码,而不是上一次正式发布。
  • 编辑/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/requests/packages/urllib3/contrib/ntlmpool.py的本地副本以逃避第38行的反斜杠。

代码应如下所示:

def __init__(self, user, pw, authurl, *args, **kwargs):
    """
    authurl is a random URL on the server that is protected by NTLM.
    user is the Windows user, probably in the DOMAIN\\username format.
    pw is the password for the user.
    """