Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requests package python is not imported

I have installed requests package on my mac (10.8) as any other with pip.

I can see it under /Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages.

However when I import requests in a python script I get a terminal error: ImportError: No module named requests as if it's not installed.

Easy install say it is installed as well:

$ 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

The only error I could locate was when upgrading with 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...

Some ideas why it cannot be imported? Thanks

like image 950
Diolor Avatar asked Feb 14 '26 07:02

Diolor


1 Answers

This appears to be a bug in urllib3. Looking at the source, starting at line 33 of the file that raised the error:

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

That \u in the middle of the string is illegal. I don't get this error from just import requests or even import requests.packages.urllib3, but if I import requests.packages.urllib3.contrib.ntlmpool, I get it too.

I don't know why it's automatically importing ntlmpool for you, but that's not important; it's definitely a bug.


The bug was fixed in urllib in change 1f7f39cb on 2013-05-22, and merged to requests in change 2ed976ea on 2013-06-08 as part of issue 1412. But it's still present in the 1.2.3 release, which is the latest version on PyPI as of 2013-07-05. You can find further information in issue 1416, which was closed fixed with the comment "The fix should be out fairly soon."

So, you have three choices:

  • Wait for 1.2.4.
  • pip install git+https://github.com/kennethreitz/requests to install the top-of-tree code off github instead of the last official release.
  • Edit your local copy of /Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/requests/packages/urllib3/contrib/ntlmpool.py to escape the backslash on line 38.

The code should look like this:

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.
    """
like image 99
abarnert Avatar answered Feb 16 '26 20:02

abarnert