Re: [Tutor] python2 vs python3 urllib

2011-04-25 Thread pierre dagenais

On 11-04-23 06:58 PM, pierre dagenais 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 
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?

Thank you,
Your help is much appreciated.
___
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Thank you everybody,

The short answer was of course:
import urllib.request
instead of :
import urllib

I realize I'm still confused about packages, libraries, modules, 
classes, etc...
Right now they're all more or less the same to me. I've got some reading 
to do.

Thanks again,

Pierre
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python2 vs python3 urllib

2011-04-23 Thread wesley chun
i strongly recommend you consider using the 2to3 tool if porting apps
from 2.x to 3.x... it will show you much more than you may think.
here's the output from the 2.7 version of 2to3:

$ 2to3 test.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored test.py
--- test.py (original)
+++ test.py (refactored)
@@ -1,5 +1,5 @@
-import urllib
-sock = urllib.urlopen("http://diveintopython.org/";)
+import urllib.request, urllib.parse, urllib.error
+sock = urllib.request.urlopen("http://diveintopython.org/";)
 htmlSource = sock.read()
 sock.close()
 print (htmlSource)
RefactoringTool: Files that need to be modified:
RefactoringTool: test.py

it shows you the diffs, and if you use the -w option, it will
overwrite (and backup) the 2.x version.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.chun : wescpy-gmail.com : @wescpy
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python2 vs python3 urllib

2011-04-23 Thread Alan Gauld


"pierre dagenais"  wrote


sock = urllib.urlopen("http://diveintopython.org/";)
AttributeError: 'module' object has no attribute 'urlopen'

What is the proper syntax in version 3?


using help() shows that urllib has become a package in v3.
The request module looked promising and sure enough


import urllib.request as req
help(req)


shows:

   urlopen(url, data=None, timeout=)
   
   urlretrieve(url, filename=None, reporthook=None, data=None)


HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python2 vs python3 urllib

2011-04-23 Thread Noah Hall
On Sat, Apr 23, 2011 at 11:58 PM, pierre dagenais  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 
>    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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] python2 vs python3 urllib

2011-04-23 Thread pierre dagenais
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 
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?

Thank you,
Your help is much appreciated.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor