Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85389 > unrolled thread

Python 3 and the requests library

Started byBrian <brian.from.fl@gmail.com>
First post2015-02-09 10:11 -0800
Last post2015-02-09 15:03 -0600
Articles 8 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Python 3 and the requests library Brian <brian.from.fl@gmail.com> - 2015-02-09 10:11 -0800
    Re: Python 3 and the requests library Brian <brian.from.fl@gmail.com> - 2015-02-09 11:20 -0800
      Re: Python 3 and the requests library Zachary Ware <zachary.ware+pylist@gmail.com> - 2015-02-09 14:11 -0600
        Re: Python 3 and the requests library Brian <brian.from.fl@gmail.com> - 2015-02-09 12:37 -0800
          Re: Python 3 and the requests library Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-09 13:58 -0700
            Re: Python 3 and the requests library Brian <brian.from.fl@gmail.com> - 2015-02-09 13:37 -0800
              Re: Python 3 and the requests library Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-09 15:42 -0700
          Re: Python 3 and the requests library Zachary Ware <zachary.ware+pylist@gmail.com> - 2015-02-09 15:03 -0600

#85389 — Python 3 and the requests library

FromBrian <brian.from.fl@gmail.com>
Date2015-02-09 10:11 -0800
SubjectPython 3 and the requests library
Message-ID<9b2a1b9d-4f1b-4786-a3a6-2d414074da6f@googlegroups.com>
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

[toc] | [next] | [standalone]


#85395

FromBrian <brian.from.fl@gmail.com>
Date2015-02-09 11:20 -0800
Message-ID<96003963-8ca1-4b62-811d-05d6f6a5709d@googlegroups.com>
In reply to#85389
I am also seeing this in my Mac Mavericks Python 3 installation when I use just the built-in logging library. Again, a tiny example executable script and the results:

$ cat test2.py
#!/usr/bin/env python3
import logging
logging.info("TEST2 starting")

$ ./test2.py
Traceback (most recent call last):
  File "./test2.py", line 3, 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__'

Googling hasn't helped track this one down. In lieu of an answer, some pointers to tools or other things to look for would be greatly appreciated. Thanks!

Brian

[toc] | [prev] | [next] | [standalone]


#85400

FromZachary Ware <zachary.ware+pylist@gmail.com>
Date2015-02-09 14:11 -0600
Message-ID<mailman.18576.1423512729.18130.python-list@python.org>
In reply to#85395
On Mon, Feb 9, 2015 at 1:20 PM, Brian <brian.from.fl@gmail.com> wrote:
> I am also seeing this in my Mac Mavericks Python 3 installation when I use just the built-in logging library. Again, a tiny example executable script and the results:
>
> $ cat test2.py
> #!/usr/bin/env python3
> import logging
> logging.info("TEST2 starting")
>
> $ ./test2.py
> Traceback (most recent call last):
>   File "./test2.py", line 3, 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__'
>
> Googling hasn't helped track this one down. In lieu of an answer, some pointers to tools or other things to look for would be greatly appreciated. Thanks!

Try this, from wherever test2.py lives:

python3 -c "import token;print(token.__file__)"

You should get back something akin to "/usr/lib64/python3.4/token.py"
(however that translates to Mac).  If instead you get the path to some
file of your own, rename your file.

Hope this helps,
-- 
Zach

[toc] | [prev] | [next] | [standalone]


#85401

FromBrian <brian.from.fl@gmail.com>
Date2015-02-09 12:37 -0800
Message-ID<97b23ac0-c363-43d0-815e-198354d39c58@googlegroups.com>
In reply to#85400
Zach,

Here is what I get on the Mac:

$ python3 -c "import token;print(token.__file__)" 
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/token.py

Just for grins, I also ran it against the built-in Python 2.7.5 version:

$ python -c "import token;print(token.__file__)" 
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/token.pyc

The only difference seems to be that the 2.7.5 version has precompiled it.

Then I followed your example but ran it against the logging module instead, as that seems to be where the problem lies. Again, for both version (broken 3 and working 2) as a comparison:

$ python3 -c "import logging;print(logging.__file__)" 
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/logging/__init__.py

$ python -c "import logging;print(logging.__file__)" 
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.pyc

But nothing is pointing to anything except what appears to be the formally installed versions of these library modules.

Brian

[toc] | [prev] | [next] | [standalone]


#85403

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-02-09 13:58 -0700
Message-ID<mailman.18578.1423515534.18130.python-list@python.org>
In reply to#85401
On Mon, Feb 9, 2015 at 1:37 PM, Brian <brian.from.fl@gmail.com> wrote:
> Zach,
>
> Here is what I get on the Mac:
>
> $ python3 -c "import token;print(token.__file__)"
> /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/token.py

Are you running this from the same directory where you have your test
scripts (in case there is also a token module hanging around in that
directory)?

[toc] | [prev] | [next] | [standalone]


#85406

FromBrian <brian.from.fl@gmail.com>
Date2015-02-09 13:37 -0800
Message-ID<d6b8b366-d6a2-4929-8e27-406b626e953d@googlegroups.com>
In reply to#85403
Thank you, Ian and Zack! That was exactly the issue. Apparently, having a token.py script (one of my first Python 2 scripts to get an authorization token from a billing server) is OK for Python 2 but breaks Python 3.

*face palm*

Thank you again so very much!

Brian

On Monday, February 9, 2015 at 3:59:11 PM UTC-5, Ian wrote:
> On Mon, Feb 9, 2015 at 1:37 PM, Brian wrote:
> > Zach,
> >
> > Here is what I get on the Mac:
> >
> > $ python3 -c "import token;print(token.__file__)"
> > /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/token.py
> 
> Are you running this from the same directory where you have your test
> scripts (in case there is also a token module hanging around in that
> directory)?

[toc] | [prev] | [next] | [standalone]


#85408

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-02-09 15:42 -0700
Message-ID<mailman.18581.1423521786.18130.python-list@python.org>
In reply to#85406
On Mon, Feb 9, 2015 at 2:37 PM, Brian <brian.from.fl@gmail.com> wrote:
> Thank you, Ian and Zack! That was exactly the issue. Apparently, having a token.py script (one of my first Python 2 scripts to get an authorization token from a billing server) is OK for Python 2 but breaks Python 3.

Local modules that have the same absolute module path as standard
library modules will cause problems in either version of Python. I
think it's unfortunate that Python files that happen to live in the
same directory as the main script automatically get treated as
top-level modules that shadow the standard library. You're not the
first person to be confused by this.

It appears that the reason this works for you in Python 2 and not in
Python 3 is because the linecache module doesn't import tokenize in
Python 2, whereas it does in Python 3. If you had tried to import
tokenize directly in Python 2 then I expect you'd have the same
problem.

[toc] | [prev] | [next] | [standalone]


#85404

FromZachary Ware <zachary.ware+pylist@gmail.com>
Date2015-02-09 15:03 -0600
Message-ID<mailman.18579.1423515836.18130.python-list@python.org>
In reply to#85401
On Mon, Feb 9, 2015 at 2:37 PM, Brian <brian.from.fl@gmail.com> wrote:
> Zach,
>
> Here is what I get on the Mac:
>
> $ python3 -c "import token;print(token.__file__)"
> /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/token.py

That looks correct.

> Just for grins, I also ran it against the built-in Python 2.7.5 version:
>
> $ python -c "import token;print(token.__file__)"
> /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/token.pyc
>
> The only difference seems to be that the 2.7.5 version has precompiled it.

The 3.4 version is also precompiled, but __file__ gives the filename
of the actual module (not the .pyc implementation detail).  You can
find precompiled token.py for 3.4 at
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/__pycache__/token.cpython-34.pyc

> Then I followed your example but ran it against the logging module instead, as that
> seems to be where the problem lies.

Not so, the traceback you posted shows pretty clearly that the problem
is in the tokenize module, where the token module it has imported does
not have an __all__ attribute.

> Again, for both version (broken 3 and working 2) as a comparison:
>
> $ python3 -c "import logging;print(logging.__file__)"
> /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/logging/__init__.py

This should have failed with the same traceback as your test2.py.

> $ python -c "import logging;print(logging.__file__)"
> /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.pyc
>
> But nothing is pointing to anything except what appears to be the formally installed
> versions of these library modules.

Try your test again.  If it fails again, run my test again at the same prompt.

-- 
Zach

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web