3

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

2
  • 1
    Are you sure your script is running under Python 3.2 and not Apple's pre-installed 2.7, or some other version? Commented Jul 5, 2013 at 8:05
  • @abarnert like that $ python3 /Users/me/Desktop/script.py ? Commented Jul 5, 2013 at 8:06

1 Answer 1

2

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.
    """
Sign up to request clarification or add additional context in comments.

3 Comments

@abamert Thanks, I just changed that file. Now the reason why it's not importing it is probably that I have messed up some packages with different python version. About time to use virtual enviroments..
The Syntax Error you've revealed is harmless in almost every case, so shouldn't affect the import. =)
This is an old post, but I'm still getting this problem in Python 37. Any updates on this? I don't see a packages folder in the requests folder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.