3

我已经在我的 mac(10.8)上安装了 requests 包,就像其他任何pip.

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

但是,当我import requests在 python 脚本中时,我得到一个终端错误: ImportError: No module named requests好像它没有安装一样。

Easy install 说它也已安装:

$ 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...

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

4

1 回答 1

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是非法的。我没有从 justimport requests或 even中得到这个错误import requests.packages.urllib3,但如果我import requests.packages.urllib3.contrib.ntlmpool,我也得到它。

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


该错误已在 2013-05-22urllib更改 1f7f39cbrequests中修复,并在2013-06-08 作为问题 1412的一部分合并到更改 2ed976ea中。但它仍然存在于 1.2.3 版本中,这是截至2013 年 7 月 5 日 PyPI 上的最新版本。您可以在issue 1416中找到更多信息,该问题已关闭并附有评论“该修复程序应该很快就会发布”。

所以,你有三个选择:

  • 等待 1.2.4。
  • pip install git+https://github.com/kennethreitz/requests从 github 上安装树顶代码,而不是最后一个官方版本。
  • 编辑您的本地副本/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.
    """
于 2013-07-05T08:10:35.677 回答