[Tutor] python2 vs python3 urllib

Noah Hall enalicho at gmail.com
Sun Apr 24 01:32:13 CEST 2011


On Sat, Apr 23, 2011 at 11:58 PM, pierre dagenais <eq742 at ncf.ca> wrote:
> The following code works as expected with python version 2.6.5, but with
> version 3.1.2 I get the following error:
>
> pierre:/MyCode/mesProjets$ py3 test.py
>
> Traceback (most recent call last):
>  File "test.py", line 3, in <module>
>    sock = urllib.urlopen("http://diveintopython.org/")
> AttributeError: 'module' object has no attribute 'urlopen'
>
> The code:
>
> #example 8.5 of diveintopython.org/
> import urllib
> sock = urllib.urlopen("http://diveintopython.org/")
> htmlSource = sock.read()
> sock.close()
> print (htmlSource)
>
> What is the proper syntax in version 3?

The problem is described in the exception - the module "urllib" has no
method urlopen. Things were moved around in 3.0. In fact,
diveintopython has some information on this here -
http://diveintopython3.org/porting-code-to-python-3-with-2to3.html#urllib


More information about the Tutor mailing list