Python 3 and the requests library

Brian brian.from.fl at gmail.com
Mon Feb 9 13:11:12 EST 2015


On the Mac running Mavericks, I have successfully managed to install and use the requests library for HTTP and HTTPS requests using Python 2. But I'd like to move to Python 3.

I downloaded the most recent stable version of Python 3 for Mac. All is well. I did a pip3 install requests command and it worked. I could then start the interpreter and perform a simple HTTP GET request:

$ python3
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  5 2014, 20:42:22) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> r = requests.get("http://www.google.com")
>>> r
<Response [200]>

So, with that success, I created an executable script filled with the same commands:

#!/usr/bin/env python3
import requests
r = requests.get("http://www.google.com")
print(r)

At first it failed because it couldn't find httplib2. Not sure why, but I installed httplib2 and now get the following error:

$ ./test.py
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    import requests
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/__init__.py", line 53, in <module>
    from .packages.urllib3.contrib import pyopenssl
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/__init__.py", line 3, in <module>
    from . import urllib3
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/__init__.py", line 10, in <module>
    from .connectionpool import (
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 2, in <module>
    import logging
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/logging/__init__.py", line 26, in <module>
    import sys, os, time, io, traceback, warnings, weakref, collections
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/traceback.py", line 3, in <module>
    import linecache
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/linecache.py", line 10, in <module>
    import tokenize
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/tokenize.py", line 40, in <module>
    __all__ = token.__all__ + ["COMMENT", "tokenize", "detect_encoding",
AttributeError: 'module' object has no attribute '__all__'

Here is the full list of installed packages that I have in python3 (the list in python2 is huge and not posted here):

$ pip3 list
httplib2 (0.9)
pip (1.5.6)
requests (2.5.1)
setuptools (2.1)
urllib3 (1.10)

Notre that if I change python3 to python in my script, it works fine using the default Python 2.7.5 version. And as a Plan B I can stay with Python 2.7.5. But I'd really like to move to Python 3 on the Mac.

Thanks in advance for any thoughts or help!

Brian



More information about the Python-list mailing list